[jdom-interest] XMLOutputter - Unwanted line breaks occurring between tags

Alex Chaffee guru at edamame.stinky.com
Fri Oct 6 09:27:51 PDT 2000

That's fine with me.  If someone else implements this (I'm a bit busy
right now), note that the check happens in two different places --
printElement and printElementContent -- so should probably be factored
into a method isStringOnly(mixedContent) or isStringOnly(Element)

Also, hmm, won't the current logic miss elements with multiple
children that all happen to be strings?

Also, if someone feels like it, there's a small glitch I didn't fix:
whitespace, meaning line breaks, in a PI, and I think inside an
attribute list, gets preserved no matter what.  So you end up with things lke

 <foo><bar><baz baf="boo"

even with trimText on.  Again, I haven't tracked down the symptoms
fully, but it shouldn't be hard to.

 - A

On Fri, Oct 06, 2000 at 10:49:39AM -0400, kendrick at us.ibm.com wrote:
> I am using XMLOutputter to output XML with line breaks, and it is working
> reasonably well.  However, I have noticed that when an element whose
> content is null is outputted, a line break occurs between the opening and
> closing tags.  I would prefer that the opening and closing tags occur on
> the same line as they do when the element's content is an empty string.
> For example, the following code:
>      root.addContent(new Element("name").setText(null));
> produces the following output:
>      <name>
>      </name>
> However the following code:
>      root.addContent(new Element("name").setText(""));
> produces the following output:
>      <name></name>
> The line break appears to occur because the test for "stringOnly" in the
> printElement() method of XMLOutputter results in false when the element's
> content is null.  I recommend modifying the test for "stringOnly" to
> include the check for a null value.  That seems to cause the output of an
> element's tags to appear on the same line when the content is null.
> protected void printElement(Element element, Writer out,
>                     int indentLevel, NamespaceStack namespaces)  throws
> IOException {
>      List mixedContent = element.getMixedContent();
>      boolean empty = mixedContent.size() == 0;
>      boolean stringOnly =
>           !empty &&
>           mixedContent.size() == 1 &&
>           (mixedContent.get(0) instanceof String || mixedContent.get(0) ==
> null);
> Sincerely,
> Shannon Kendrick
> IBM e-business Integration Services - Atlanta, GA
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/

More information about the jdom-interest mailing list