• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

White Space Problems while reading contents of XML

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Iam tring to get the values of an XML using the following code.

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringElementContentWhitespace(true);
System.out.println(domFactory.isIgnoringElementContentWhitespace());
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("samplexml.xml");

When i was trying to print the Node Names under FistElement,I get


true
#text
ChildElement1
#text
ChildElement2
#text



The following is the XML Content....

<Elements>
<FirstElement>
<ChildElement1>shf</ChildElement1>
<ChildElement2>zxcv</ChildElement2>
<FirstElement>
</Elements>


But when i execute the same code with the above xml defined in the same line,then iam getting (CORRECTLY)

ChildElement1
ChildElement2


can anyone provide me a solution to ignore the White Spaces in the XML???

Thanks in advance
Senthil
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the easiest way to ignore the whitespace is to write some if-statements in your code that ignore it.

The harder way is to attach a schema or a DTD to the document and make sure that schema or DTD makes the whitespace ignorable. Then your boldfaced line of code will work. But I would just write the if-statements.
 
Senthil Kumar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.I have already implemented the code with IF condition.I just wanna know what is the alternative way of avoiding the white spaces.

Thanks for your reply

Regards
Senthil
reply
    Bookmark Topic Watch Topic
  • New Topic