Re: Help with XML processing using DOM
- From: "Jeff Higgins" <oohiggins@xxxxxxxxx>
- Date: Thu, 28 Jun 2007 18:57:39 -0400
"Jeff Higgins" <oohiggins@xxxxxxxxx> wrote in message
news:utWgi.37$6N3.12@xxxxxxxxxxxxxxx
Also note that every Element node contains a Text node, so that
kernco wrote:
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()
When I try to cast the Node to an Element, I get a runtime error:
Exception in thread "main" java.lang.ClassCastException:
com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast
to org.w3c.dom.Element
You're not on an Element node you're on a Text node.
As you iterate the children nodes you'll have to check
what type of node you're currently referencing.
See the table at:
<http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html>
Element <b> is probably the next sibling of the Text node you're
printing out. Also when you print the value of this Text node it
is equal to a single whitespace because parser collapses all
whitespace to a single space unless you tell it not to.
.
- Follow-Ups:
- Re: Help with XML processing using DOM
- From: Joshua Cranmer
- Re: Help with XML processing using DOM
- References:
- Help with XML processing using DOM
- From: kernco
- Re: Help with XML processing using DOM
- From: Jeff Higgins
- Re: Help with XML processing using DOM
- From: kernco
- Re: Help with XML processing using DOM
- From: Jeff Higgins
- Help with XML processing using DOM
- Prev by Date: Re: Help with XML processing using DOM
- Next by Date: Need help with running examples in JAXB in the JWSDP
- Previous by thread: Re: Help with XML processing using DOM
- Next by thread: Re: Help with XML processing using DOM
- Index(es):