Help with XML processing using DOM
- From: kernco <razael1@xxxxxxxxx>
- Date: Thu, 28 Jun 2007 09:01:50 -0000
I'm trying to use XML files as input to my program. I decided to use
the DOM (DocumentBuilder, Node, etc.) instead of SAX. I have loaded
the file into an instance of Document, call it 'doc', and now I want
to go through and extract the data.
XML file:
<?xml version="1.0"?>
<a>
<b>
<c>foo</c>
<d>foo</d>
<d>foo</d>
</b>
<e>
</e>
</a>
Relevant code:
Node domNode = doc.getDocumentElement();
System.out.println(domNode.getNodeName());
domNode = domNode.getFirstChild();
System.out.println(domNode.getNodeName());
do {
if (domNode.getNodeName().equals("b")) {
...
} else if ...
...
} while ((domNode = domNode.getNextSibling()) != null);
You will notice my println statements there to help figure out the
problem. The first one prints "a" as expected, I'd then expect the
first child to be "b", but instead it prints "#text". I changed the
second println to print the node value instead of name, and it prints
a single whitespace character. Why isn't this getting the Node at
"b"?
Thanks,
Colin
.
- Follow-Ups:
- Re: Help with XML processing using DOM
- From: Jeff Higgins
- Re: Help with XML processing using DOM
- Prev by Date: Re: Sort Method Conversion
- Next by Date: Re: Help with XML processing using DOM
- Previous by thread: Sort Method Conversion
- Next by thread: Re: Help with XML processing using DOM
- Index(es):