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");
            doc.appendChild(results);

            Class.forName("com.mysql.jdbc.Driver");
            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");
                results.appendChild(row);
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs.getObject(i);
                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            }
            stmt.close();
            conn.close();
            rs.close();

            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");
  }
%>
<html>
  <head></head>
  <body>
    <h2><font color='lilac'>CP Agent</font></h2>

    <table border="2">
      <tr>
                <th>CP_Agent_Id</th>
                <th>Algorithm</th>
                <th>Priority</th>
                <th>Speed</th>
                <th>Size</th>
            </tr>
        <%
          Element  element = doc.getDocumentElement();
          NodeList personNodes = element.getChildNodes();
          for (int i=0; i<personNodes.getLength(); i++){
            Node emp = personNodes.item(i);
            if (isTextNode(emp))
              continue;
            NodeList NameDOBCity = emp.getChildNodes();
        %>
      <tr>
        <%
          for (int j=0; j<NameDOBCity.getLength(); j++ ){
            Node node = NameDOBCity.item(j);
            if ( isTextNode(node))
              continue;
            Node firstChild = node.getFirstChild();
String textnode;
if (firstChild == null) textnode = null;
else textnode = firstChild.getNodeValue();
        %>
        <td><%= textnode %></td>
        <%
          }
        %>
      </tr>
        <%
          }
        %>
    </table>
  </body>
</html>

and my xml file is below.

<cpa>
<agent>
<planning_agent_id>PLN24</planning_agent_id>
<ALGORITHM>Greedy</ALGORITHM>
<priority>3</priority>
<speed>2</speed>
<size>1</size>
</agent>
<agent>
<planning_agent_id>PLN06</planning_agent_id>
<ALGORITHM>A*</ALGORITHM>
<priority>NA</priority>
<speed>NA</speed>
<size>NA</size>
</agent>
</cpa>

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, networking, learning, and sharing knowledge.