Hi,

I have the following code that displays the node element with the corresponding node value. I want to store the values in an array in reference to the node name.

i.e.
XML (my xml is much bigger than this, 300 elements):
Code:
  
<stock>
    <symbol>SUNW</symbol>
    <price>17.1</price>
  </stock>
data[symbol] = SUNW;
data[price] = 17.1;

please help

test.jsp
Here's my source code:
Code:
<html>
<head>
<title>dom parser</title>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="dombean.*" %>
</head>

<body bgcolor="#ffffcc">

<center>
<h3>Pathways Info</h3>
<table border="2" width="50%">

<jsp:useBean id="domparser" class="dombean.MyDomParserBean" />

<%
Document doc = domparser.getDocument("c:/stocks/stocks.xml");
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("MOTHER-OCC-YRS-PREVIOUS")) {
               //out.println("</tr>");
             //}
             out.println("<tr><td>"+elementName+"</td>");
             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></tr>");
                
              //}
            }
         }
    }
%>
</table>
</body>
</html>
MyDomParserBean.java
Code:
package dombean;

import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.io.*;

public class MyDomParserBean 
implements java.io.Serializable {
   public MyDomParserBean() {
   }

   public static Document 
   getDocument(String file) throws Exception {
         
    // Step 1: create a DocumentBuilderFactory
     DocumentBuilderFactory dbf =
      DocumentBuilderFactory.newInstance();

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

    // Step 3: parse the input file to get a Document object
     Document doc = db.parse(new File(file));
     return doc;
   }      
}