This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes how to ignore whitespace in xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to ignore whitespace in xml " Watch "how to ignore whitespace in xml " New topic
Author

how to ignore whitespace in xml

ved gunjan
Ranch Hand

Joined: May 17, 2003
Posts: 86
Hi!Experts
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();
System.out.println(configNode);
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
<sample><config>
<userid>Jack</userid>
<password>secret</password>
<header>reroute
other
<b>Random</b> text.</header>
<location>California</location>
</config></sample>

But when I change the xml file to sumething like this....
<sample>
<config>
<userid>Jack</userid>
<password>secret</password>
<header>reroute other<b>Random</b>text.</header>
<location>California</location>
</config>
</sample>
Im getting following error
[sample: null]
[#text:
]
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
Regards
Ved Gunjan
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I'd suggest trying to make your code adapt to situations where you might encounter Text nodes in between Element nodes.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to ignore whitespace in xml
 
Similar Threads
Recreate XML file
Reading UTF-8 encoded Data in JSP using DOM
HSSF
Efficient XML Parsing
Writing XML element