[jdom-interest] XMLOutputter(Format.getCompactFormat)

Lighton Phiri lighton.phiri at gmail.com
Wed Apr 11 02:40:08 PDT 2012


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" ?>


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


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
            XMLOutput.output(XMLDocument, System.out);

            JDOMRawOutputFile = new FileWriter(new File("jdom_recipes.xml"));
            XMLOutput.output(XMLDocument, JDOMRawOutputFile);
        catch(IOException ioe) {
        catch(JDOMException jdome) {


More information about the jdom-interest mailing list