2014-02-11 3 views
1

DOM 파서를 사용하여 Lingvo xml 사전을 구문 분석하려고합니다.DOM 파서가 서브 노드를 보지 못합니다.

문제점 : card 노드의 하위 노드를 참조하지 않음 DOM 파서 (아래 코드 참조).

질문? card 노드에서 wordtranslation 노드를 끌어 방법

내 코드 :

import entity.Item; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

public class DOMParser { 

    public void parseXMLFile(String xmlFilePath) throws IOException, SAXException { 
     Document document = builder.parse(ClassLoader.getSystemResourceAsStream(xmlFilePath)); 
     List<Item> itemList = new ArrayList<Item>(); 
     NodeList nodeList = document.getDocumentElement().getChildNodes(); 
     //iterates through cards 
     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node node = nodeList.item(i); 
      System.out.println(node.getNodeName()); 
      if (node instanceof Element) { 
       if ("card".equals(node.getNodeName())) { 
        // HERE node hasn't got anything!!! I mean attributes, childs etc. 
       } 
      } 
     } 
    } 
} 

내 XML :

<?xml version="1.0" encoding="UTF-16"?> 
<dictionary formatVersion="5" title="User ;vocabulary_user1" sourceLanguageId="1058" destinationLanguageId="1033" nextWordId="611" targetNamespace="http://www.abbyy.com/TutorDictionary"> 
    <statistics readyMeaningsQuantity="90" activeMeaningsQuantity="148" learnedMeaningsQuantity="374" /> 
    <card> 
     <word>загальна цікавість</word> 
     <meanings> 
      <meaning> 
       <statistics status="4" answered="122914" /> 
       <translations> 
        <word>genaral wondering</word> 
       </translations> 
      </meaning> 
     </meanings> 
    </card> 
</dictionary> 
+1

확인이 http://www.mkyong.com/java/how-to-read-xml- file-in-java-dom-parser/.... 노드와 그 값에 접근하는 방법 – Naren

+0

@Naren 이미이 튜토리얼을 읽었습니다. http://www.javacodegeeks.com/2013/05/parsing-xml- using-dom-sax-and-stax-parser-in-java.html –

답변

2

중첩 된 for 루프가 발생하지 않고 모든 내용을 읽을 수있는 재귀 방식을 사용할 수 있습니다. 당신의 XML의

: XML 파싱의 기본에 대한

public static void main(String[] args) throws ParserConfigurationException, 
      SAXException, IOException { 
     InputStream path = new FileInputStream("dom.xml"); 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document document = builder.parse(path); 
     traverse(document.getDocumentElement()); 

    } 

    public static void traverse(Node node) { 
     NodeList list = node.getChildNodes(); 
     for (int i = 0; i < list.getLength(); i++) { 
      Node currentNode = list.item(i); 
      traverse(currentNode); 

     } 

     if (node.getNodeName().equals("word")) { 
      System.out.println("This -> " + node.getTextContent()); 
     } 

    } 

주는,

This -> загальна цікавість 
This -> genaral wondering 
관련 문제