繁体   English   中英

如何查找具有特定属性值的子元素

[英]How to find a child element with specific attribute value

我习惯使用C ++库进行xml解析,即rapidxml和tinyxml。 构建在DOM解析器中的java对我来说绝对没有意义。 经过一番努力,我能够存储我需要的根节点,但现在我想找到一个具有特定id的标签。

这是一个示例xml:

<?xml version="1.0" encoding="UTF-8"?>
<adventure title="Test Adventure" author="Tester">
    <enemies>
        <enemy id="e1" difficulty="1">Enemy1</enemy>
    </enemies>
    <story>
        <narration id="n1" action="d1">It's adventure time!</narration>
        <decision id="d1">
            <description>Would you like to participate?</description>
            <choice action="n2">Yes, please.</choice>
            <choice action="n3">Not the slightest.</choice>
        </decision>
        <narration id="n2" action="end">Great choice!</narration>
        <narration id="n3" action="end">Okay...</narration>
    </story>
</adventure>

我将<story>节点存储在Node实例中。 从那里,我想找到具有特定id的节点(假设'n3'节点)。

我想用这个方法:

private Node findNode(String id) {
    Node node = storyNode.getFirstChild();
    while ( node != null ) {
        Element elem = (Element)node;
        if ( elem.getAttribute("id") == id ) {
            return node;
        }
        node = node.getNextSibling();
    }

    return null;
}

这将是我使用C ++库的方式,但这离工作还很远......我不明白为什么我要为这么简单的任务进行类型转换。 为什么Node不能通过指定属性名来获取属性的方法。 getFirstChild()似乎甚至没有返回第一个孩子......

我在网上找到的包装类要么是30循环深,要么更难使用。 为什么这么复杂? 我究竟做错了什么?

为什么API如此...真棒...将归结为它的设计原因和方式。 解决方案可能比您想象的更简单。

可用的功能之一是XPath实现,它被吹捧为XML的查询语言。

因此,您可以简单地使用类似//*[@id='n1']来查找id属性等于n1任何节点(例如),而不是尝试遍历每个节点和每个子节点。

使用您的示例XML和以下代码...

try {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Test.xml"));
    Node root = doc.getDocumentElement();

    XPath xPath = XPathFactory.newInstance().newXPath();

    XPathExpression xExpress = xPath.compile("//*[@id='n1']");
    NodeList nl = (NodeList)xExpress.evaluate(root, XPathConstants.NODESET);

    System.out.println("Found " + nl.getLength() + " matches");
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException ex) {
    ex.printStackTrace();
}

这输出

Found 1 matches

看一下XPath的更多细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM