#####################################
#  XML ÆĽÌ, Document »ý¼º, ÆÄÀÏ·Î ÀúÀå ¿¹Á¦ (okjsp)                       
#####################################


¼³¸í :

  XMLÀ» ó¸®ÇÏ´Â °£´ÜÇÑ ¿¹Á¦

################################# ################################# #################################

/*
 * Created on 2004. 7. 21.
 */
package net.okjsp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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


/**
 * @author kenu
 */
public class XmlHandler {

    public static void main(String[] args) throws JDOMException, IOException {
        XmlHandler handler = new XmlHandler();
        String file = "/tmp/foo.xml";
        handler.makeXmlFile(handler.makeDomWithElement(), file);
        Document doc = handler.readXmlFile(file);
        Element element = doc.getRootElement();
        System.out.println(element.getChild("user").getChild("id").getText());
    }

    /**
     * @return
     * 
     */
    private Document makeDomWithElement() {
//      This builds: This is the root
        Document doc = new Document();
        Element e = new Element("root");
        Element user = new Element("user");
        Element id = new Element("id");
        id.setText("userid");
        Element pass = new Element("pass");
        pass.setText("password");
        
        user.addContent(id);
        user.addContent(pass);

        e.addContent(user);
        doc.setRootElement(e);
        
        return doc;
    }
    
    /**
     * @throws IOException
     */
    private void makeXmlFile(Document doc, String file) throws IOException {
//      Raw output
        XMLOutputter outp = new XMLOutputter();
        outp.setIndent("  ");
        outp.setNewlines(true);
        outp.setTextTrim(true);

        FileWriter fw = new FileWriter(file);
        if (doc != null) {
            outp.output(doc, fw );
        }
    }
    
    private Document readXmlFile(String file) throws JDOMException {
        
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new File(file));
        return doc;
    }
}



#################################
# 
#################################