File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

documentBuilderFactory parse problem

 
R Ang
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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>
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic