Here's some jsp code that will parse a xml file and display its contents in a
HTML table


*******************Start******************
<%@ page import="java.sql.*,javax.xml.parsers.*,org.w3c.dom.*,java.io.*"%>

<%  //Parse a XML file and print its contents using DOM

    String xmlFile = "\\financialpassport\\basic_data.xml";
    //create a DocumentBuilderFactory
     DocumentBuilderFactory dbf =  DocumentBuilderFactory.newInstance();

    //create a DocumentBuilder
     DocumentBuilder db = dbf.newDocumentBuilder();

    //parse the input file to get  a Document object
     Document doc = db.parse(new File(xmlFile));
%>
<html>
<head>
<title>Your Title Here</title>
<body bgcolor="#ffffcc">

<center>
<h3>whatever you want as heading </h3></h3>
<table border="2" width="50%">
<%
traverseTree(doc, out);
%>

<%! private void traverseTree(Node node,JspWriter out) throws Exception {
        if(node == null) {
           return;
        }
        int type = node.getNodeType();

        switch (type) {
           // handle document nodes
           case Node.DOCUMENT_NODE: {
             out.println("<tr>");
             traverseTree(((Document)node).getDocumentElement(),
             out);
             break;
          }
          // handle element nodes
          case Node.ELEMENT_NODE: {
            String elementName = node.getNodeName();
             if(elementName.equals("record")) {
               out.println("</tr><tr>");
             }
             NodeList childNodes = node.getChildNodes();
             if(childNodes != null) {
                int length = childNodes.getLength();
                    for (int loopIndex = 0; loopIndex < length ; loopIndex++)
                {
                 traverseTree(childNodes.item(loopIndex),out);
                }
              }
              break;
           }
           // handle text nodes
           case Node.TEXT_NODE: {
              String data = node.getNodeValue().trim();

              if((data.indexOf("\n")  <0) && (data.length() > 0)) {
                out.println("<td>"+data+"</td>");
              }
            }
         }
    }
%>
</body>
</html>

*******************End******************

In the section "case Node.ELEMENT_NODE: " replace records with the tag_name
you want to begin parsing


hope this helps
santosh








Dmitry Namiot <[EMAIL PROTECTED]> on 10/30/2001 08:32:09 AM

Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Santosh Daryani/IT/Aon Consulting)

Subject:  Re: Need Some Help with xml and jsp!



>What is the best way to read a xml docuement in jsp going node to node.

Check out Coldtags suite on http://www.servletsuite.com/jsp.htm
XMLTextReader taglib does this job.
--
Coldbeans Software - server-side Java (tm) components
http://www.servletsuite.com



__________________________________________________________________
Your favorite stores, helpful shopping tools and great gift ideas. Experience
the convenience of buying online with Shop@Netscape!
http://shopnow.netscape.com/

Get your own FREE, personal Netscape Mail account today at
http://webmail.netscape.com/

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to