File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes DOM and whitespace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM and whitespace" Watch "DOM and whitespace" New topic
Author

DOM and whitespace

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I'm using xerces to parse an XML document. Do you always have to check the node type to ensure that you are actually getting an element when you are traversing through the document? For example, if I have the xml
<root>
<numbers>
</one>
</two>
</numbers>
</root>
and i am at the <root> and I do get ChildNodes, I get a nodelist consisting of numbers. If I immediately get the childnodes of that, then i always get #text returned instead of <one> or <two>. So i have to loop through the child nodes until i find an NODE.ELEMENT_NODE to be sure I have <one> or <two>.
Is there anyway to ignore the whitespace so you can just use the api calls directly without having to loop through the nodelist all the time to get what you want?
Hope that makes sense. Thanks
Brian
Dhinakar
Greenhorn

Joined: Aug 28, 2001
Posts: 1
Brian,
Did you try setIgnoringElementContentWhitespace(true) in the DocumentBuilderFactory.
DocumentBuilderFactory.setIgnoringElementContentWhitespace(true)

------------------
Dhina


Dhina
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
Yes, that still leaves me with a bunch of #text nodes. I am just using the below method:

Is there a way to just get the elementNodes? Otherwise I am always looping through the nodelist to find an node type of element_node which seems pretty wasteful...
Thanks
Brian
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Brian,
To use Dhina's trick, you need to have a DTD, otherwise your parser cannot determine that your element has an "element only content model".
Another alternative (only if you use a DOM Level 2 Core compliant DOM implementation) would be to use a Node iterator or a Tree walker with the help of the DocumentTraversal interface which is implemented through the Document object. Below is an example using the Node iterator:

Cheers,
Beno�t
[This message has been edited by Beno�t d'Oncieu (edited August 29, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM and whitespace