使用 XPATH java 提取 XML 嵌套内容 [英] extract XML nested contents using XPATH java

查看:28
本文介绍了使用 XPATH java 提取 XML 嵌套内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 pom.xml 中的依赖项标记提取 xml 嵌套内容.但是即使指定了位置,它也没有获得正确的dependenices 标记.有什么帮助吗?这是我的 pom.xml

im trying to extract xml nested contents for my dependencies tag in my pom. But it is not getting the correct dependenices tag eventhough the location is specified. any help? This is my pom.xml

<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    
    
    <build>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-nodeps</artifactId>
                        <version>1.8.1</version>
                    </dependency>

                </dependencies>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.intent.tm</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.0.0.1</version>
        </dependency>
        <!-- For Compress JS -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPSJca</artifactId>
            <version>7.0</version>
        </dependency>

这是我的代码

private static String nodeToString(Node node) throws TransformerException {
        StringWriter buf = new StringWriter();
        Transformer xform = TransformerFactory.newInstance().newTransformer();
        xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xform.transform(new DOMSource(node), new StreamResult(buf));
        return (buf.toString());
    

}

File fXmlFile = new File(prop.getProperty("testFile"));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document document;
        Node result = null;
       
            document = dbf.newDocumentBuilder().parse(fXmlFile);
            XPath xPath = XPathFactory.newInstance().newXPath();
            String xpathStr = "//project//dependencies";
            result = (Node) xPath.evaluate(xpathStr, document, XPathConstants.NODE);
            log.info(nodeToString(result));

实际输出:

<dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-nodeps</artifactId>
                        <version>1.8.1</version>
                    </dependency>

                </dependencies>

预期输出:

<dependencies>
        <dependency>
            <groupId>com.intent.tm</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.0.0.1</version>
        </dependency>
        <!-- For Compress JS -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPSJca</artifactId>
            <version>7.0</version>
        </dependency>

出于某种原因,当指定的位置不是 build 时,它会在 build 标签中打印依赖项.

For some reason it is printing the dependencies in the build tag when the location specified is not build..im confused why this is happening and is there anyway to get my expected out?

推荐答案

在 XPath 中 // 可以匹配任意数量的子路径,因此它也可以在构建部分中找到依赖项.如果我改变你的代码说

In XPath // can match any number of subpaths, so it finds dependencies inside the build section too. If I change your code to say

 String xpathStr = "/project/dependencies";

我得到了你想要的输出.一个斜杠是精确匹配 XML 中的一级,因此我们从根获取项目,然后直接在项目下获取依赖项.

I get the output you wanted. One slash is to match exactly one level in the XML, so we get project from the root, then dependencies straight under project.

这篇关于使用 XPATH java 提取 XML 嵌套内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆