Where The Streets Have No Name

JDom을 이용한 XMLOutputter 본문

Developement/Java

JDom을 이용한 XMLOutputter

highheat 2010. 3. 31. 16:51
import java.io.File;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XMLOutputterDemo2 {

    public static void main(String[] args) {
        try {
            SAXBuilder b = new SAXBuilder(false);  // true -> validate

            // Create a JDOM document.
            Document doc = b.build(new File("e:/people.xml"));

            // Create an output formatter, and have it write the doc.
            //new XMLOutputter().output(doc, System.out);
            XMLOutputter xo = new XMLOutputter();
            Format f = xo.getFormat();
            f.setEncoding("UTF-8");
            f.setIndent(" ");
            f.setLineSeparator("\r\n");
            f.setTextMode(Format.TextMode.TRIM);
            xo.setFormat(f);
            xo.output(doc, System.out);
            
        } catch (JDOMException jex) {
            System.out.print("PARSE ERROR: " + jex.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}