Java中使用Xpath进行XML解析以获取所有值匹配的元素

发布于 2025-01-16 05:41:55 字数 2520 浏览 0 评论 0原文

我有一个没有特定结构的动态 xml 文件。例如;

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:ietf:params:xml:ns:yang:yin:1" name="ui-leaf-type-second">
    <yang-version value="1.1"/>
    <namespace uri="ui-leaf-type-second"/>
    <prefix value="uil-type-second"/>
    <description>
        <text>This is basic yang types example</text>
    </description>
    <revision date="2022-02-02">
        <description>
            <text>initial revision</text>
        </description>
    </revision>
    <typedef name="laser-range">
        <type name="uint8">
            <range value="0 .. 100"/>
        </type>
        <description>
            <text>Percentage</text>
        </description>
        
    </typedef>
    <grouping name="grp-range">
        <leaf name="name-of-range">
            <type name="boolean"/>
        </leaf>
    </grouping>
    <container name="cnt-range">
        <uses name="grp-range"/>
    </container>
</module>

我想获得具有我搜索价值的完整元素。 例如,我想获取一个具有 @name = "laser-range" 的元素,

它应该返回整个元素:

<typedef name="laser-range">
        <type name="uint8">
            <range value="0 .. 100"/>
        </type>
        <description>
            <text>Percentage</text>
        </description>

</typedef>

如果我搜索 @name='name-of-range'< /strong>,它应该返回

<leaf name="name-of-range">
    <type name="boolean"/>
</leaf>

我尝试使用 xpath 获取那些,但它只返回标记中的值。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse(tmpFile.toFile());

            XPath xpath = XPathFactory.newInstance().newXPath();
            // XPath Query for showing all nodes value
            XPathExpression expr = xpath.compile("//*[@name='laser-range']");

            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            //result includes only "Percentage"
}

我还尝试使用 xpath 的 @* 表示法,但它也无法给我一个解决方案。

那么,我怎样才能做到呢?有人可以告诉我一种方法吗?

I have a dynamic xml file that has not specific structure. For example;

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:ietf:params:xml:ns:yang:yin:1" name="ui-leaf-type-second">
    <yang-version value="1.1"/>
    <namespace uri="ui-leaf-type-second"/>
    <prefix value="uil-type-second"/>
    <description>
        <text>This is basic yang types example</text>
    </description>
    <revision date="2022-02-02">
        <description>
            <text>initial revision</text>
        </description>
    </revision>
    <typedef name="laser-range">
        <type name="uint8">
            <range value="0 .. 100"/>
        </type>
        <description>
            <text>Percentage</text>
        </description>
        
    </typedef>
    <grouping name="grp-range">
        <leaf name="name-of-range">
            <type name="boolean"/>
        </leaf>
    </grouping>
    <container name="cnt-range">
        <uses name="grp-range"/>
    </container>
</module>

I want to get full element which has value that I searching for.
For example, I want to get an element that have @name = "laser-range"

It should return whole element:

<typedef name="laser-range">
        <type name="uint8">
            <range value="0 .. 100"/>
        </type>
        <description>
            <text>Percentage</text>
        </description>

</typedef>

If I search for @name='name-of-range', it should return

<leaf name="name-of-range">
    <type name="boolean"/>
</leaf>

I tried to get those with xpath but it is returning only values in the tag.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse(tmpFile.toFile());

            XPath xpath = XPathFactory.newInstance().newXPath();
            // XPath Query for showing all nodes value
            XPathExpression expr = xpath.compile("//*[@name='laser-range']");

            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            //result includes only "Percentage"
}

I also tried to use @* notation of xpath but it could not give me a solution too.

So, how can I make it? Can someone to show me a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文