본문 바로가기

JDOM4

[Java] JDom – SAXBuilder를 이용한 Document 객체 생성 XML 소스(소켓, 파일, 입력, 등)을 입력 받아서 각 항목에 접근하기 위해서는 먼저 Document 객체를 생성해야 한다. 다음은 다양한 입력 소스로 부터 Document 객체를 생성하는 방법이다.…// 바이트배열에 담김 데이터를 이용해서 XML Document 객체를 생성한다. byte[] responseBody = null;if (responseBody != null) { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(new ByteArrayInputStream(new String(responseBody).getBytes())); root = doc.getRootElement(); resultMode = root.. 2013. 11. 15.
[Java] JDom – XMLOutputter 를 이용한 Document 객체 출력 작성된 XML Document 객체를 다양한 형태로 출력할 수 있다.… Document doc new Document(); … // Document 객체 작성 … XMLOutputter xo = new XMLOutputter(); // 1) Document 객체를 표준출력으로 보낸다. xo.output(doc, System.out); // 2) Document 객체를 파일로 보낸다. xo.output(doc, new FileOutputStream(“XML파일”)); xo.output(doc, new FileWriter(“XML파일”)); // 3) Document 객체를 소켓 OutputStream 으로 보낸다. os = new DataOutputStream(소켓.getOutputStream()); xo.. 2013. 11. 15.
[Java] – JDom XML 문서의 주석(Comment)을 다는 방법 XML 문서 작성시, 문서의 맨 앞에 주석을 달고 싶을 때는 Document객체에 Comment 객체를 생성해서 추가해 주면 된다.… Comment comment = new Comment( “xml 문서에 대한 설명” ); doc.getContent().add( 0, comment );…* doc.addContent( comment ) 를 이용해서 주석을 추가하면 문서의 마지막에 추가된다. 2013. 11. 15.
[Java] – JDom Element 생성시 CDATA 처리 Element 를 생성해서 값을 할당할때, CDATA 객체를 생성해서 할당해 주면 된다.… import org.jdom.Element; import org.jdom.CDATA;… …… Element eleItem = new Element(“TITLE”); eleItem.addContent( new CDATA(“제목 입니다.”) );…* Element 를 생성해서 값을 할당 시, setText() 메소드를 사용할 수도 있다. 단, 이 방법으로는 CDATA 처리가 불가능하다. eleItem.setText( “제목 입니다.” ); 2013. 11. 15.