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

Brett McLaughlin brett.mclaughlin at lutris.com
Fri Oct 6 09:45:48 PDT 2000

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.

Hi Kendrick -

  I made some changes to XMLOuputter early this week that should ensure
that getContent() never returns null - could you get the latest version
from CVS today and see if the problem goes away, and let us know either
way? Thanks...

If you still have problems, I 'll track them down.


> 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

Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 

More information about the jdom-interest mailing list