• 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

documentBuilderFactory parse problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create an XML document using the JDK 1.5 DocumentBuilderFactory class. I am using the following statement to read the sample XML string below and create an XML document.
The problem I'm having is with the way the input document is parsed. The FIELD2 element in the output document is an empty element. I need the spaces from the input document to remain in the output.
I checked the documentBuilderFactory.isIgnoringElementContentWhitespace() and it is set to FALSE (default).
How can I parse this document and retain elements with blank text values?

Thanks


CODE:

Document resultDocument = documentBuilder.parse(new InputSource(
new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(inputXMLString.getBytes())))));


INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE >
<MESSAGE_DATA>
<FIELD1>SOME TEXT</FIELD1>
<FIELD2> </FIELD2>
<FIELD3>SOME TEXT</FIELD3>
</MESSAGE_DATA>
</MESSAGE>


OUTPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE >
<MESSAGE_DATA>
<FIELD1>SOME TEXT</FIELD1>
<FIELD2/>
<FIELD3>SOME TEXT</FIELD3>
</MESSAGE_DATA>
</MESSAGE>
 
Marshal
Posts: 28226
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
Since all the other white space in your document is being preserved, my guess is that there actually is no space between the FIELD2 start tag and end tag.
 
R Ang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No a space actually does exist. This is just an example but there are other documents with more than just one space and they all result in empty elements. To confirm this I have printed the string containing the XML before parsing and the spaces are there.
 
Paul Clapham
Marshal
Posts: 28226
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
Here's my code:

It doesn't do what your code does, instead it preserves the space.

(I removed a lot of the excess baggage in the part of the code which creates
the DOM, but that shouldn't make any difference.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic