큰 xml 파일에 sax 파서를 사용하고 싶습니다. 핸들러는 다음과 같습니다색소폰 파싱 - 중첩 된 태그를 기본 태그에 매핑
DefaultHandler handler = new DefaultHandler() {
String temp;
HashSet <String> xml_Elements = new LinkedHashSet <String>();
HashMap < String, Boolean > xml_Tags = new LinkedHashMap < String, Boolean >();
HashMap < String, ArrayList <String>> tags_Value = new LinkedHashMap < String, ArrayList <String>>();
//###startElement#######
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
xml_Elements.add(qName);
for (String tag: xml_Elements) {
if (qName == tag) {
xml_Tags.put(qName, true);
}
}
}
//###########characters###########
public void characters(char ch[], int start, int length) throws SAXException {
temp = new String(ch, start, length);
}
//###########endElement############
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (xml_Tags.get(qName) == true) {
if (tags_Value.containsKey(qName)) {
tags_Value.get(qName).add(temp);
tags_Value.put(qName, tags_Value.get(qName));
} else {
ArrayList <String> tempList = new ArrayList <String>();
tempList.add(temp);
//tags_Value.put(qName, new ArrayList<String>());
tags_Value.put(qName, tempList);
}
//documentWriter.write(qName+":"+temp+"\t");
for (String a: tags_Value.keySet()) {
try {
documentWriter.write(tags_Value.get(a) + "\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
xml_Tags.put(qName, false);
}
tags_Value.clear();
}
};
내 XML과 같은 것입니다 :
<TermInfo>
<A>1/f noise</A>
<B>Random noise</B>
<C>Accepted</C>
<D>Flicker noise</D>
<F>Pink noise</F>
<I>1-f</I>
<I>1/f</I>
<I>1/f noise</I>
<I>1:f</I>
<I>flicker noise</I>
<I>noise</I>
<I>pink noise</I>
<ID>1</ID>
</TermInfo>
<TermInfo>
<A>3D printing</A>
<B>Materials fabrication</B>
<C>Accepted</C>
<D>3d printing</D>
<F>2</F>
<I>three dimension*</I>
<I>three-dimension*</I>
<I>3d</I>
<I>3-d</I>
<I>3d*</I>
</TermInfo>
내가 .. 각 A의 태그 A. 아래의 B, C, D와 I를 즉 모든 중첩 된 태그를 클러스터 싶어 함께 .. 등.하지만 위의 처리기를 사용하여 출력 ABCDII 같은 것입니다. 각 A에 대해 하나의 객체를 만들고 그 안에 다른 요소를 추가 할 수 있습니까? 어떻게 이것을 포함시킬 수 있습니까?
대단히 감사드립니다.이 솔루션 만 좋아했습니다. 감사합니다 :) – Kaira
또한 qName 대신 여기에 localName. qName은 출력을주었습니다.이 두 가지 차이점은 무엇입니까? – Kaira
작동하는 경우 정답으로 표시하십시오. qName과 localName의 차이점은 http://stackoverflow.com/questions/7157355/what-is-the-difference-between-localname-and-qname을 참조하십시오. 필자가 사용하지 않은 XML의 고급 개념입니다. – ProgrammersBlock