Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

getCompactFormat() only prints first content #72

Closed
rolfl opened this Issue · 0 comments

1 participant

@rolfl
Collaborator

From Phiri - http://markmail.org/message/rja2qddsrqaelgyj

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());
        }
    }
}
@rolfl rolfl closed this issue from a commit
@rolfl rolfl Fixes #72 - whitespace separated content causes 'Compact' XMLOutputte…
…r to ignore content after the whitespace.
8182ccf
@rolfl rolfl closed this in 8182ccf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.