간단히 만들어본 샘플
파일 시스템도 유니코드, 인코딩도 유니코드로 저장
기존에 작성했던 부분을 수정...ㅎㅎ
펼쳐두기..
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class TestInfo {
public static void main(String[] args) {
Element rootElement = new Element("root");
Document doc = new Document(rootElement);
rootElement.setAttribute(new Attribute("속성", "여긴 속성값"));
rootElement.addContent(new Element("메이커").addContent("현대"));
rootElement.addContent(new Element("모델").addContent("아반떼XD"));
rootElement.addContent(new Element("년식").addContent("2006"));
rootElement.addContent(new Element("색상").addContent("silver"));
rootElement.addContent(new Element("품번").addContent("HD200601"));
rootElement.addContent(new Comment("주석이 들어갈 부분 "));
/** 특정 노드를 제거 **/
Element yearElement = rootElement.getChild("년식");
boolean removed = rootElement.removeChild("년식");
/** XML Write **/
Format f = Format.getCompactFormat();
f.setEncoding("utf-8");
f.setIndent(" ");
XMLOutputter op = new XMLOutputter(f);
try {
FileOutputStream fileOut = new FileOutputStream("c:/jdom_test.xml");
OutputStreamWriter writer = new OutputStreamWriter(fileOut, "utf-8");
op.output(doc, writer);
writer.close();
} catch(IOException ioe ) {
System.out.println("IOE : " + ioe.fillInStackTrace());
}
}
}
No comments:
Post a Comment