DOM解析器没有看到子节点 [英] DOM parser doesn't see subnodes
本文介绍了DOM解析器没有看到子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题: DOM解析器没有看到卡的子节点
节点(见下面的代码)。
问题?: 如何拉字
c code code code code code code code code code code $ strong>我的代码:
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)) ;
列表< Item> itemList = new ArrayList< Item>();
NodeList nodeList = document.getDocumentElement()。getChildNodes();
//通过卡片迭代
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没有任何东西!我的意思是属性,孩子等
}
}
}
}
}
我的xml:
<?xml version =1.0encoding =UTF-16?>
< dictionary formatVersion =5title =User; vocabulary_user1sourceLanguageId =1058destinationLanguageId =1033nextWordId =611targetNamespace =http://www.abbyy.com/TutorDictionary> ;
< statistics readyMeaningsQuantity =90activeMeaningsQuantity =148learnMeaningsQuantity =374/>
<卡>
< word>загальнацікавість< / word>
< meaning>
<含义>
< statistics status =4answers =122914/>
< translations>
< word> genaral wondering< / word>
< / translations>
< / meaning>
< / meaning>
< / card>
< / dictionary>
解决方案
您可以使用递归方法阅读所有
对于您的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();
文档文档= builder.parse(path);
traverse(document.getDocumentElement());
}
public static void traverse(Node node){
NodeList list = node.getChildNodes(); (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());
}
}
给,
此 - > загальнацікавість
这个 - > genaral想知道
I trying to parse Lingvo xml dictionary with help of DOM parser.
Problem: DOM parser doesn't see the subnodes of card
node (see code below).
Question?: How to pull word
and translation
nodes from card
node
My code:
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.
}
}
}
}
}
My 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>
解决方案
You can use a recursive approach to read through all your contents without getting into the mess of nested for
loops.
For your 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());
}
}
Gives,
This -> загальна цікавість
This -> genaral wondering
这篇关于DOM解析器没有看到子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文