I have this JSP code that i found online and it seems to work cuz it loops thru the xml file and writes the HTML code for the TRs and TDs but for some reason is not displaying the data. Am i missing something?

<%@page import="org.w3c.dom.*, javax.xml.parsers.*" %>
<%
  DocumentBuilderFactory docFactory = 
  DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.parse
("http://www.somesite.com/file.xml");
%>
<%!
  public boolean isTextNode(Node n){
    return n.getNodeName().equals("#text");
  }
%>
<html>
  <head><title></title></head>
  <body>    
    <table border="2">
      <tr>
        <th>Name of Employee</th>
        <th>Date of Birth</th>
        <th>City</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;
        %>
        <td><%= node.getFirstChild().getNodeValue() %></td>
        <%
          } 
        %>
      </tr>
        <%
          }
        %>
    </table>
  </body>
</html>

Recommended Answers

All 2 Replies

Nevermind the above code as it was a mess.
I have this other code and it actually pulls the data and displays it but my problem now is how can i tell it with nodes to display as opposed to displaying all nodes and i only want to display the first 10 records?

<%@ taglib uri="http://java.sun.com/jstl/xml"  prefix="x" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

    <c:import url="http://www.causes.com/agc/daily_leaderboard" var="causeXml" />
    <x:parse xml="${causeXml}" varDom="parsedXml" />

    <table border="1">
      <tr><td>Rank</td><td>Cause</td><td>Donations</td></tr>

      <x:forEach select="$parsedXml/hash/leaderboard" var="currentCause">
        <x:forEach select="*">
      <tr>
          <td><x:out select="." /></td>
      </tr>
        </x:forEach>
      </x:forEach>
    </table>

You need to manage "processing" done by forEach. This command just blindly output anything its found in the file. So either you need to filter data which should be displayed or which should be rejected. Or perhaps forward received data for further processing. Without knowing what exactly you want to achieve and what resources you using we can hardly advice on it...

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.