Hi, I have to retrieve data from database and create a XML file, then i have to parse this file and display output in browser. Creating XML file works fine. But parsing is not working. No errors. But output is not displayed in browser. Have submission tomo. Please Please help. Thanks in advance.

<%@page language="java"%>
<%@page import=" java.sql.*"%>
<%@page import=" java.util.*"%>
<%@page import=" java.lang.String"%>
<%@page import=" java.io.StringWriter"%>
<%@page import=" java.io.File"%>
<%@page import=" javax.xml.parsers.DocumentBuilder"%>
<%@page import=" javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import=" javax.xml.transform.OutputKeys"%>
<%@page import=" javax.xml.transform.Transformer"%>
<%@page import=" javax.xml.transform.TransformerFactory"%>
<%@page import=" org.w3c.dom.Document"%>
<%@page import=" org.w3c.dom.Element"%>
<%@page import=" javax.xml.transform.dom.DOMSource"%>
<%@page import=" javax.xml.transform.stream.StreamResult"%>
<%@page import=" org.w3c.dom.Node"%>
<%@page import=" org.w3c.dom.NodeList"%>

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.newDocument();
            Element results = doc.createElement("cpa");

            String userName = "root";
            String password = "";
            String url = "jdbc:mysql://localhost/masdata";
            Connection conn = DriverManager.getConnection(url, userName, password);
            Statement stmt = conn.createStatement();
            String cpa_idd = request.getParameter("cpa_id");
            String algm = request.getParameter("algo");
            String priorityy = request.getParameter("priority");
            String speedd = request.getParameter("speed");
            String sizee = request.getParameter("size");
            stmt.executeUpdate("insert into planning_agent values('" + cpa_idd + "','" + algm + "','" + priorityy + "','" + speedd + "','" + sizee + "');");
            ResultSet rs = stmt.executeQuery("select *from planning_agent");

            ResultSetMetaData rsmd = rs.getMetaData();
            int colCount = rsmd.getColumnCount();

            while (rs.next()) {
                Element row = doc.createElement("agent");
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs.getObject(i);
                    Element node = doc.createElement(columnName);

            DOMSource domSource = new DOMSource(doc);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            StreamResult sr = new StreamResult("C:/Users/Shruthi/Documents/NetBeansProjects/WebApplication6/web/cpa.xml");
            transformer.transform(domSource, sr);

            doc = builder.parse("C:/Users/Shruthi/Documents/NetBeansProjects/WebApplication6/web/cpa.xml");

  public boolean isTextNode(Node n){
    return n.getNodeName().equals("#text");
    <h2><font color='lilac'>CP Agent</font></h2>

    <table border="2">
          Element  element = doc.getDocumentElement();
          NodeList personNodes = element.getChildNodes();
          for (int i=0; i<personNodes.getLength(); i++){
            Node emp = personNodes.item(i);
            if (isTextNode(emp))
            NodeList NameDOBCity = emp.getChildNodes();
          for (int j=0; j<NameDOBCity.getLength(); j++ ){
            Node node = NameDOBCity.item(j);
            if ( isTextNode(node))
            Node firstChild = node.getFirstChild();
String textnode;
if (firstChild == null) textnode = null;
else textnode = firstChild.getNodeValue();
        <td><%= textnode %></td>

and my xml file is below.


It Works fine till doc = builder.parse("C:/Users/Shruthi/Documents/NetBeansProjects/WebApplication6/web/c­pa.xml");

Move all computation stuff connection to DB and building XML to servlet. These tasks do not belong to JSP/page view. You do so and you will find your life easier on debugging

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.