Where The Streets Have No Name

JDom - xml파일 생성 본문

Developement/Java

JDom - xml파일 생성

highheat 2010. 4. 6. 15:26
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class CdrXmlDemo1 {

    public static void main(String[] args) {
        Element root = new Element("cdr");

        Element message = new Element("message");
        root.addContent(message);

        Element messageId = new Element("message-id");
        messageId.setText("111222333");
        message.addContent(messageId);

        Element from = new Element("from");
        from.setText("8222112517");
        message.addContent(from);

        Element to = new Element("to");
        to.setText("8222112515");
        message.addContent(to);

        Element description = new Element("description");
        description.setText("MM1_notification.REQ");
        message.addContent(description);

        Element msc = new Element("msc");
        message.addContent(msc);
        Element pduData = new Element("pdu-data");
        msc.addContent(pduData);
        
        Document doc = new Document(root);
        // serialize it into a file
        try {
            FileOutputStream out = new FileOutputStream("msc.xml");
            XMLOutputter serializer = new XMLOutputter();

            Format f = serializer.getFormat();
            f.setEncoding("UTF-8");
            f.setIndent(" ");
            f.setLineSeparator("\r\n");
            f.setTextMode(Format.TextMode.TRIM);
            serializer.setFormat(f);

            serializer.output(doc, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}