상세 컨텐츠

본문 제목

StAX 방식의 XML 생성 (StAX create xml example code java)

프로그램 언어/Java

by husks 2020. 11. 13. 17:42

본문

반응형


XML을 생성할때 평소 DOM 방식으로 생성 했습니다.


그런데 대량의 XML 을 만들다 보니 메모리에 부족해서 오류가 발생 하였습니다.


확인을 해보니 StAX(Streaming API for XML) 방식이 있는겁니다.


자세한건 아래 링크 참고

https://ko.wikipedia.org/wiki/StAX


XML을 읽는 소스는 많은데 작성하는 소스는 별로 없습니다.


암튼 아래와 같은 구조의 XML 을 생성할때 참고할 만한 소스 예제를 작성해 보았습니다.


자세한 내용은 주석을 참고해 주세요.



StaxWriter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.FileOutputStream;
import java.util.ArrayList;
 
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
 
public class StaxWriter {
 
    public static void main(String[] args) throws Exception {
 
        String filePath = "D:/test/library.xml";
 
        // Create a XMLOutputFactory
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        // Create XMLEventWriter
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(filePath),"UTF-8");
        // Create a EventFactory
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent newLine = eventFactory.createDTD("\n"); //개행
        XMLEvent tab = eventFactory.createDTD("\t"); //탭
 
        // XML 시작
        StartDocument startDocument = eventFactory.createStartDocument();
        eventWriter.add(startDocument);
        eventWriter.add(newLine);
 
        // Library 태그 시작
        StartElement libraryElement = eventFactory.createStartElement("""""Library");
        eventWriter.add(libraryElement);
        eventWriter.add(newLine);
 
        //BookInfo 태그 시작
        eventWriter.add(tab); //탭
        eventWriter.add(eventFactory.createStartElement("""""BookInfo"));
        eventWriter.add(newLine); //개행
 
        //Title 태그
        eventWriter.add(tab);eventWriter.add(tab);
        eventWriter.add(eventFactory.createStartElement("""""Title"));//Title 태그 시작
        eventWriter.add(eventFactory.createCharacters("태백산맥 "));
        eventWriter.add(eventFactory.createEndElement("""""Title"));//Title 태그 종료
        eventWriter.add(newLine);
 
        //MainWriter 태그
        eventWriter.add(tab);eventWriter.add(tab);
        eventWriter.add(eventFactory.createStartElement("""""MainWriter"));//MainWriter 태그 시작
        eventWriter.add(eventFactory.createCharacters("조정래 "));
        eventWriter.add(eventFactory.createEndElement("""""MainWriter"));//MainWriter 태그 종료
        eventWriter.add(newLine);
 
 
        Attribute attr = null;
        ArrayList<Attribute> attrList = null;
 
        eventWriter.add(tab);eventWriter.add(tab);
        eventWriter.add(eventFactory.createStartElement("""""SeriesInfo"));//SeriesInfo 태그 시작
        eventWriter.add(newLine);
 
        for(int i=0;i<3;i++) {
 
            //속성
            attr = eventFactory.createAttribute("id", Integer.toString(i)); //값 입력
            attrList = new ArrayList<Attribute>();
            attrList.add(attr);
 
            //Series 태그
            eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);
            eventWriter.add(eventFactory.createStartElement("""""Series", attrList.iterator(), null));//Series 태그 시작
            eventWriter.add(newLine);
 
            //제목
            eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);
            eventWriter.add(eventFactory.createStartElement("""""Name"));    //Name 태그 시작
            eventWriter.add(eventFactory.createCharacters("태백산맥_"+i));        //Name 값 입력
            eventWriter.add(eventFactory.createEndElement("""""Name"));        //Name 태그 시작
            eventWriter.add(newLine);
            //작가
            eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);
            eventWriter.add(eventFactory.createStartElement("""""Writer"));
            eventWriter.add(eventFactory.createCharacters("조정래"));
            eventWriter.add(eventFactory.createEndElement("""""Writer"));
            eventWriter.add(newLine);
            //가격        
            eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);
            eventWriter.add(eventFactory.createStartElement("""""Price"));
            eventWriter.add(eventFactory.createCharacters("1000"));
            eventWriter.add(eventFactory.createEndElement("""""Price"));
            eventWriter.add(newLine);
 
            //Series 태그
            eventWriter.add(tab);eventWriter.add(tab);eventWriter.add(tab);
            eventWriter.add(eventFactory.createEndElement("""""Series"));//Series 태그 종료
            eventWriter.add(newLine);
        }
 
        eventWriter.add(tab);eventWriter.add(tab);
        eventWriter.add(eventFactory.createEndElement("""""SeriesInfo"));//SeriesInfo 태그 종료
        eventWriter.add(newLine);
 
 
        eventWriter.add(tab);
        eventWriter.add(eventFactory.createEndElement("""""BookInfo"));//BookInfo 태그 종료
        eventWriter.add(newLine);
 
 
        eventWriter.add(eventFactory.createEndElement("""""Library"));//Library 태그 종료
        eventWriter.add(newLine);
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();
    }
 
}



반응형

관련글 더보기

댓글 영역