java - DOM 解析器看不到子节点

标签 java xml parsing dom xml-parsing

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

问题: DOM 解析器看不到 card 节点的子节点(请参阅下面的代码)。

问题?:如何从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>

最佳答案

您可以使用递归方法来阅读所有内容,而无需陷入困惑的嵌套 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());
        }

    }

给予,

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

关于java - DOM 解析器看不到子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703474/

相关文章:

java - 元件坐标

c# - WCF REST 网络服务 : getting request data when the content is not xml valid

xml - 验证 XSD 文件时出现问题 : The content type of a derived type and that of its base must both be mixed or both be element-only

命令行解析器,端口号检索

java - Android (PhoneGap) - 从推送通知中按钮的点击事件发送 HTTP 请求

java - Android Google Map v2 - 在 map 上显示当前位置以及自定义标记的推荐方法是什么?

android - AndroidManifest.xml文件的编码问题

javascript - JSON解析: Uncaught SyntaxError: Unexpected token e

parsing - 如何使用 Data.ByteString 解析 7GB 文件?

java - 优化 GWT 片段加载序列