Re: Help with XML processing using DOM




"kernco" <razael1@xxxxxxxxx> wrote in message
news:1183021310.434884.270330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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


try Element.getTagName()


.



Relevant Pages

  • Re: Help with XML processing using DOM
    ... the DOM (DocumentBuilder, Node, etc.) instead of SAX. ... Node domNode = doc.getDocumentElement; ... When I try to cast the Node to an Element, ...
    (comp.lang.java.help)
  • Help with XML processing using DOM
    ... I'm trying to use XML files as input to my program. ... the DOM (DocumentBuilder, Node, etc.) instead of SAX. ... Node domNode = doc.getDocumentElement; ... You will notice my println statements there to help figure out the ...
    (comp.lang.java.help)
  • Re: javax.xml.parsers.DocumentBuilder ohne DTD validation?
    ... einzustellen). ... Aber wie gesagt - aber SAX will ich nicht ... ... der DocumentBuilder gibt mir mit wenigen Zeilen ein DOM; ... Es geht mir wirklich darum, wie man dem DocumentBuilder beibringt, ...
    (de.comp.lang.java)
  • javax.xml.parsers.DocumentBuilder ohne DTD validation?
    ... DocumentBuilder nutzen ohne DTD checks? ... wie ich einen Sax Parser entsprechend ... aber ich hab jetzt halt schon den DocumtBuilder based Code (der sich ... kann ich mit Sax ähnlich einfach an ein DOM Objekt kommen? ...
    (de.comp.lang.java)