Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

store XML node value into an array with node element name

843834Sep 24 2007 — edited Sep 25 2007
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):
-----
<stock>
<symbol>SUNW</symbol>
<price>17.1</price>
</stock>
-----
would store the following:
*data[symbol] = SUNW;*
*data[price] = 17.1;*

Thanks in advance,

Tony

test.jsp
Here's my source 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) &#38;&#38; (data.length() > 0)) {
out.println("<td>"+data+"</td></tr>");
//}
}
}
}
%>
</table>
</body>
</html>
{code}
-----
*MyDomParserBean.java*
Code: package dombean;
-----
{code:java}
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;
}      
}
{code}
Edited by: ynotlim333 on Sep 24, 2007 8:41 PM

Edited by: ynotlim333 on Sep 24, 2007 8:44 PM

Edited by: ynotlim333 on Sep 24, 2007 8:45 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 23 2007
Added on Sep 24 2007
3 comments
448 views