我试图解析XML Lingvo的字典与DOM解析器的帮助。
问题:DOM解析器不见的子节点card
节点(见下面的代码)。
问?:如何拉word
和translation
从节点card
节点
我的代码:
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>