2013. 1. 31. 16:34

라이브러리 다운로드 : http://www.jdom.org/

★ XML 생성 순서

1. Document 생성

: Document document = new Document();

2. root Element 생성

: Element root = new Element("루트");

3. 기타 Element 생성

: Element e1 = new Element("첫번째엘리먼트");

4. Document에 root 추가

: document.setRootElement(root);

5. 상위 노드에 하위 노드 추가

: root.addContent(e1);

6. 상위 노드에 값 데이터 설정

: e1.setText("값");

결과 - 일단은 Document 타입

<?xml version="1.0" encoding="UTF-8"?>
<루트><첫번째엘리먼트>값</첫번째엘리먼트></루트>

★ XMLOutputter를 이용항 Document 객체 출력

1. XMLOutputter 객체 생성

XMLOutputter outputter = new XMLOutputter();

2. Format 객체 생성 - 가독성을 고려

Format format = outputter.getFormat();

2-1. 인코딩, 들여쓰기, 줄바꿈 설정

format.setEncoding("UTF-8");

format.setIndent("\t");

format.setLineSeparator(LineSeparator.DEFAULT);

2-2. textMode 설정

format.setTextMode(Format.TextMode.NORMALIZE);

※ PRESERVE : 원본 유지

TRIM : 좌우 여백 제거

NORMALIZE : 좌우 여백 제거 + 내부 여백을 한칸 여백으로

TRIM_FULL_WHITE :

3. XMLOutputter 에 Format 설정

outputter.setFormat(format);

4. 객체 출력

String으로 출력

: outputter.outputString(document);

표준 출력

: outputter.output(document, System.out);

파일로 출력

: outputter.output(document, new FileoutputStream("이름.확장자");

: outputter.output(document, new FileWriter("이름.확장자");

소켓으로 출력

: stream = new DataOutputStream(소켓.getOutputStream());

outputter.output(document, stream);

결과 - String 타입으로 변환. 가독성이 높아짐.

<?xml version="1.0" encoding="UTF-8"?>

<루트>

<첫번째엘리먼트>값</첫번째엘리먼트>

</루트>

 

http://blog.naver.com/comkwang17/140176506563

'ApplicationPrograming > Java' 카테고리의 다른 글

Commons-DbUtils  (0) 2013.03.15
Commons-Fileupload의 한글관련 문제 추가  (0) 2013.03.15
me2DAY XML Parsing (DOM) - 자바 XML파싱  (0) 2012.07.24
랜덤 숫자 생성  (0) 2012.06.11
jar파일 실행  (0) 2012.05.04
Posted by 물색없는세상