[jdom-interest] XMLOutputter(Format.getCompactFormat)
Lighton Phiri
lighton.phiri at gmail.com
Wed Apr 11 02:40:08 PDT 2012
Hello,
I recently started playing around with JDOM (I am working with ) and
was trying to test XMLOutputter. For some strange reason, when I
attempt to write to an XML file using
XMLOutputter(Format.getCompactFormat), the code below only writes the
first sub-element of the root node. Substituting getCompactFormat with
the other formats (getPrettyFormat and getRawFormat) results in
desired output.
Could there be something I might be missing?
Input File
<?xml version="1.0" encoding="UTF-8" ?>
<people>
<person>
<firstname>Aaa</firstname>
<lastname>Bbb</lastname>
<age>20</age>
</person>
<person>
<firstname>Ccc</firstname>
<lastname>Ddd</lastname>
<age>30</age>
</person>
<person>
<firstname>Eee</firstname>
<lastname>Fff</lastname>
<age>40</age>
</person>
<person>
<firstname>Ggg</firstname>
<lastname>Hhh</lastname>
<age>50</age>
</person>
</people>
Output
<?xml version="1.0" encoding="UTF-8"?>
<people><person><firstname>Aaa</firstname></person></people>
Code
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
// jdom2 imports
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class FirstRecipeJDOM {
public static void main(String[] args) {
File XMLFile = new File("file.xml");
FileWriter JDOMRawOutputFile = null;
SAXBuilder builder = new SAXBuilder();
try {
Document XMLDocument = builder.build(XMLFile);
XMLOutputter XMLOutput = new
XMLOutputter(Format.getCompactFormat());
XMLOutput.output(XMLDocument, System.out);
JDOMRawOutputFile = new FileWriter(new File("jdom_recipes.xml"));
XMLOutput.output(XMLDocument, JDOMRawOutputFile);
}
catch(IOException ioe) {
System.out.println(ioe.getMessage());
}
catch(JDOMException jdome) {
System.out.println(jdome.getMessage());
}
}
}
--
Phiri
More information about the jdom-interest
mailing list