Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DOM解析器不';看不到子节点_Java_Xml_Parsing_Dom_Xml Parsing - Fatal编程技术网

Java DOM解析器不';看不到子节点

Java DOM解析器不';看不到子节点,java,xml,parsing,dom,xml-parsing,Java,Xml,Parsing,Dom,Xml Parsing,我试图在DOM解析器的帮助下解析Lingvo xml字典 问题:DOM解析器没有看到card节点的子节点(请参见下面的代码) 问题?:如何从card节点拉取word和translation节点 我的代码: 导入实体项; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList; 导入org.xml.sax.SAXException; 导入javax.xml.parsers.D

我试图在DOM解析器的帮助下解析Lingvo xml字典

问题:DOM解析器没有看到
card
节点的子节点(请参见下面的代码)

问题?:如何从
card
节点拉取
word
translation
节点

我的代码:

导入实体项;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类DOMParser{
public void parseXMLFile(字符串xmlFilePath)引发IOException、SAXException{
documentdocument=builder.parse(ClassLoader.getsystemresourceastream(xmlFilePath));
List itemList=new ArrayList();
NodeList NodeList=document.getDocumentElement().getChildNodes();
//反复浏览卡片
for(int i=0;i
我的xml:


загальна цікавість
一般疑问

您可以使用递归方法读取所有内容,而不会陷入嵌套的
for
循环的混乱状态

对于您的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());
        }

    }

检查此项以了解解析xml的基础知识。。。。如何访问节点及其子节点value@Naren我已经读过这个教程了
<?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>
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