[jdom-interest] XMLOutputter options

Joseph Bowbeer jozart at csi.com
Sat Oct 28 20:27:38 PDT 2000


Request for enhancement to XMLOutputter:

Add a property for controlling the newline(s) after the xml declaration.

This configuration should be separate from the element content "pretty"
formatting, since one affects content and the other doesn't.

For example, consider XMLOutputter's default output for a document that
already contains its own formatting:

<?xml version="1.0" encoding="UTF-8"?><Person>
    <name>Jane Smith</name>
</Person>

I'd like to be able to add 2 newlines after the xml declaration --
without adding newlines to the element content.


On the other hand, in the interest of consolidation, I still think that
constructing XMLOutputter from smaller pieces might be a good idea in
the long run (see below).

In this refactoring, the newline-after-declaration setting would be part
of XMLWriter, while the newline-in-content setting would be part of the
XMLFilter.  XMLOutputter would exist as a convenient shell to coordinate
these other components.

  // XMLOutputter = SAXOutputter+XMLFilter+XMLWriter

  import org.jdom.*;
  import org.jdom.output.SAXOutputter;
  import samples.sax.DataFormatFilter;
  import samples.sax.XMLWriter;

  Element person = new Element("Person");
  Element name = new Element("name");
  name.setText("Jane Smith");
  person.addContent(name);
  Document doc = new Document(person);

  DataFormatFilter filter = new DataFormatFilter();
  filter.setContentHandler(new XMLWriter());
  filter.setIndentStep(2);
  new SAXOutputter(filter).output(doc);

--

  <?xml version="1.0" standalone="yes"?>

  <Person>
    <name>Jane Smith</name>
  </Person>

--
Joe Bowbeer






More information about the jdom-interest mailing list