2016-12-19 1 views
0

큰 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에 대해 하나의 객체를 만들고 그 안에 다른 요소를 추가 할 수 있습니까? 어떻게 이것을 포함시킬 수 있습니까?

답변

1

나는 이것이 당신이 요구하는 라인을 따른 것이라고 생각합니다. HashMap 객체의 List를 생성합니다. TermInfo가 시작될 때마다 새로운 HashMap이 생성됩니다. TermInfo 안에있는 각 endElement는 값을 Map에 넣습니다. endElement가 TermInfo 인 경우 fieldMap을 null로 설정하므로 중간 태그가 추가되지 않습니다. "TermInfo"는 설명에서 A를 나타냅니다.

public class TestHandler extends DefaultHandler 
{ 
Map<String, String> fieldMap = null; 
List<Map<String, String>> tags_Value = new ArrayList<Map<String, String>>(); 
String temp; 

// ###startElement####### 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException 
{ 
    if (localName.equals("TermInfo")) // A 
    { 
     fieldMap = new HashMap<String, String>(); 
     tags_Value.add(fieldMap); 
    } 
} 

// ###########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 (fieldMap != null) 
    { 
     if (!localName.equals("TermInfo")) // A 
     { 
      fieldMap.put(localName, temp); 
     } 
     else 
     { 
      //END of TermInfo 
      fieldMap = null; 
     } 
    } 

} 
+0

대단히 감사드립니다.이 솔루션 만 좋아했습니다. 감사합니다 :) – Kaira

+0

또한 qName 대신 여기에 localName. qName은 출력을주었습니다.이 두 가지 차이점은 무엇입니까? – Kaira

+0

작동하는 경우 정답으로 표시하십시오. qName과 localName의 차이점은 http://stackoverflow.com/questions/7157355/what-is-the-difference-between-localname-and-qname을 참조하십시오. 필자가 사용하지 않은 XML의 고급 개념입니다. – ProgrammersBlock

관련 문제