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

kendrick at us.ibm.com kendrick at us.ibm.com
Fri Oct 6 07:49:39 PDT 2000

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:

However the following code:
     root.addContent(new Element("name").setText(""));
produces the following output:

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) ==

Shannon Kendrick

IBM e-business Integration Services - Atlanta, GA

More information about the jdom-interest mailing list