Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to ignore whitespace in xml  RSS feed

ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to parse a xml file using JAXP.In the application im also using xpath to find and traverse the xml file.
The probelm is that the method setIgnoringElementContentWhitespace()is not working.

Here is the code....
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
Element root = doc.getDocumentElement();
Node configNode = root.getFirstChild();
NodeList childNodes = configNode.getChildNodes();
System.out.println("There are "+childNodes.getLength()
+" nodes in this document.");
for (int childNum = 0;childNum < childNodes.getLength();childNum++)
if ( childNodes.item(childNum).getNodeType() == Node.ELEMENT_NODE )
Element child = (Element) childNodes.item( childNum ) ;
if ( child.getTagName().equals( "header" ) )
// Do something with the header
System.out.println("Got a header!");
System.out.println("Header contents:" +
getTextContents( childNodes.item( childNum ) ) );

This is my xml file
<b>Random</b> text.</header>

But when I change the xml file to sumething like this....
<header>reroute other<b>Random</b>text.</header>
Im getting following error
[sample: null]
There are 0 nodes in this document.
Now, using the helper function findValue:
reroute othertext.
so, childNodes.getLength() is giving me o nodes when the xml form changes.

Even after using dbf.setIgnoringElementContentWhitespace(true);

Can any one plz tel me how to ignore the whitespace in the xml file.
If possible plz give a code snippet.
Many thanks in advance
Ved Gunjan
Lasse Koskela
Posts: 11962
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest trying to make your code adapt to situations where you might encounter Text nodes in between Element nodes.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!