简体   繁体   English

使用 XPATH java 提取 XML 嵌套内容

[英]extract XML nested contents using XPATH java

im trying to extract xml nested contents for my dependencies tag in my pom.我试图在我的 pom.xml 中为我的依赖项标签提取 xml 嵌套内容。 But it is not getting the correct dependenices tag eventhough the location is specified.但即使指定了位置,它也没有得到正确的依赖标签。 any help?有什么帮助吗? This is my pom.xml这是我的 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>

this is my code这是我的代码

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));

Actual output:实际 output:

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

                </dependencies>

Expected output:预期 output:

<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>

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?出于某种原因,当指定的位置不是构建时,它会在构建标签中打印依赖项。我很困惑为什么会发生这种情况,无论如何有没有让我预期的结果出来?

In XPath // can match any number of subpaths, so it finds dependencies inside the build section too.在 XPath //可以匹配任意数量的子路径,因此它也在构建部分中找到依赖关系。 If I change your code to say如果我改变你的代码说

 String xpathStr = "/project/dependencies";

I get the output you wanted.我得到了你想要的 output。 One slash is to match exactly one level in the XML, so we get project from the root, then dependencies straight under project.一个斜线是与 XML 中的一个级别完全匹配,因此我们从根目录获取项目,然后直接在项目下获取依赖项。

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

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