[jdom-interest] BUG: XMLOutputter inserts extra empty lines

Alex Rosen arosen at silverstream.com
Tue Nov 27 08:28:29 PST 2001


This should probably be in the FAQ...

When you're reading in the XML file, the JDOM tree that it builds contains
all the newlines and indents that are in the file. Then, when you're writing
out the file with XMLOutputter, you're asking it to add newlines and
indents. So, you're getting extras. The newlines and indents features of
XMLOutputter are designed for the second thing you mentioned - when
composing an XML document in memory, you can use these features  to make it
more readable when you save it to disk.

This just goes to prove the adage that all whitespace handling in XML is a
pain.

If I remember correctly, there's been some work post-beta-7 to make
XMLOutputter more robust to this problem, so you could use the same
XMLOutputter settings to nicely write out both types of documents
(pre-formatted and not pre-formatted). Is that right? Is the other Alex
still around to comment?

Alex Rosen
SilverStream Software

> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Andriy Palamarchuk
> Sent: Wednesday, November 21, 2001 10:06 AM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] BUG: XMLOutputter inserts extra empty lines
>
>
> When I read an XML file and save it back with
> XMLOutputter the result document contains extra empty
> lines before some closing tags which are on separate
> lines.
> However, there is no extra empty lines when I compose
> exactly the same document as JDom object in code and
> then print it with XMLOutputter. Obviously, this is
> some problem in the outputter with handling space
> between tags.
>
> I use JDom Beta 7.
>
> Please, let me know in case I missed something.
> Is there any workaround for this problem?
>
> Code which reads/writes XML:
>
> SAXBuilder builder = new SAXBuilder();
> Document doc = builder.build(
>     new File("employees.xml"));
>
> XMLOutputter outputter = new XMLOutputter();
> outputter.setNewlines(true);
> outputter.setIndent(true);
> outputter.setTextNormalize(true);
> String docStr = outputter.outputString(doc);
> System.out.println(docStr);
>
> The original XML file:
> <?xml version="1.0" encoding="UTF-8"?>
>
> <employees>
>   <employee id="1" title="developer"
> first-name="first-name1" last-name="last-name1">
>     <picture>apalamarchuk.jpeg</picture>
>     <profile>Design, coding, debugging; goto
> design;</profile>
>   </employee>
>   <employee id="2" title="developer"
> first-name="first-name2" last-name="last-name2">
>     <picture>mmiller.jpeg</picture>
>     <profile>Design, coding, debugging, riding bike;
> goto design;</profile>
>   </employee>
>   <employee id="3" title="manager"
> first-name="first-name3" last-name="last-name3">
>     <picture>mbaara.jpeg</picture>
>     <profile>Manage employees 1 and 2.</profile>
>   </employee>
> </employees>
>
> The resulting XML file:
> <?xml version="1.0" encoding="UTF-8"?>
> <employees>
>   <employee id="1" title="developer"
> first-name="first-name1" last-name="last-name1">
>     <picture>apalamarchuk.jpeg</picture>
>     <profile>Design, coding, debugging; goto
> design;</profile>
>
>   </employee>
>   <employee id="2" title="developer"
> first-name="first-name2" last-name="last-name2">
>     <picture>mmiller.jpeg</picture>
>     <profile>Design, coding, debugging, riding bike;
> goto design;</profile>
>
>   </employee>
>   <employee id="3" title="manager"
> first-name="first-name3" last-name="last-name3">
>     <picture>mbaara.jpeg</picture>
>     <profile>Manage employees 1 and 2.</profile>
>
>   </employee>
>
> </employees>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
> http://geocities.yahoo.com/ps/info1
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list