[jdom-interest] round trip XML

Patrick Dowler Patrick.Dowler at nrc.ca
Mon Oct 16 10:51:46 PDT 2000

I am having trouble getting IO to correctly do a round-trip. if I create a
simple Document and write it I get nice output:

XMLOutputter putter = new XMLOutputter("  ",true);
putter.output(doc, System.out);

This gives the expected & desired output, say:

<?xml version="1.0" encoding="UTF-8"?>

However, if I read the above XML using a SAXBuilder(File) and then immediately
write it out the Document with an XMLOutputter, I get either:

(1) if the newlines==true, I get an extra blank line except after the
DocType, ie:

<?xml version="1.0" encoding="UTF-8"?>



This is because the "\n"s in the xml file are kept as separate Elements
in the Document, presumably.

(2) if newlines==false, I don't get the extra blank lines but <foo> is in the
wrong place (obviously, wrong here is aesthetic, rather than technical) 

<?xml version="1.0" encoding="UTF-8"?><foo>

I understand why this happens. I realize that some legal XML will not be
preserved under a round trip (ie. <foo></foo> could be replaced by <foo/>
at the discretion of the code). Also, the root Element in the Document is
foo, so the "other" content of the Document (the "\n" after the DocType)
gets dropped silently. Is that really other content in XML? I know Document
can only have one Element, so I see it having to be dropped...

RFE: Maybe the outputter could add a newline after the DocType anyway,
or this could be turned on as distinct from the newlines arg/setting? 



Patrick Dowler
Canadian Astronomy Data Centre

More information about the jdom-interest mailing list