1

我正在使用 xpath 通过过滤 maven pom.xml 的 artefactID 来获取插件的兄弟姐妹。

下面是我的 xml 。使用 Xpath 表达式//plugin/artifactId[text()="test-maven"]我能够过滤元素,但我想获得像“配置”和里面的孩子。我尝试以这种方式修改我的 Xpath

//plugin/artifactId[text()=\"test-maven\"]/../executions/execution/configuration 。它返回元素内的所有文本,我无法遍历或获取特定的节点名称和值.

谁能帮助我解决这个问题

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.xxx.test</groupId>
    <artifactId>xxx-test</artifactId>
    <version>0.0.20-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>xxx-test</name>

    <properties>
        <platform.version>2.3.3</platform.version>
        <android.sdk.version>3</android.sdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <!--  <version>${platform.version}</version> -->
            <version>17</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <androidManifestFile>${project.build.directory}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>17</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                 <groupId>org.xxxsource</groupId>
                  <artifactId>test-maven</artifactId>
                  <version>0.0.1</version>
                  <executions>
                    <execution>
                        <id>Maven plugin</id>
                        <phase>install</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                       <configuration>
                                <fileToUpload>${project.build.directory}/${project.artifactId}-${project.version}.apk</fileToUpload> 
                                <!-- <secretKey>b3b47266-2d07-44d2-a4c4-23a64a2c9ff8</secretKey> 
                                 <uploadKey>e707d458-c9ae-4ce5-887f-8c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba</uploadKey> -->

                                 <secretKey>b3b47266-211111111d07-44d2-a41111111c4-23a64a2c9ff8</secretKey> 
                                 <uploadKey>e707d458-c9ae-4ce111111111115-887f-8111111111111111c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba</uploadKey>

                                <releaseNote>TEST</releaseNote> 
                                <grantUsers>
                                    <grantUser>c273ae88-2314-4d85-a7e5-2c2cd40638a8</grantUser>
                                    <grantUser>22a8d3df-7724-4941-8bd4-a735a807bc99</grantUser>
                                </grantUsers>
                       </configuration> 
                     </execution>
                </executions>          
            </plugin>
        </plugins>
    </build>
</project>

这是我正在使用的代码:-

                    Document doc = builder.parse(file);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(path);
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        for (int i = 0; i < nodes.getLength(); i++) {
            final Node node = (Node) nodes.item(i);
            System.out.println("Node  Name " + node.getNodeName());
            // System.out.println("Node  " + node.getTextContent());

            if (node.hasChildNodes()) {
                System.out.println("HAS Child");

                NodeList nodes1 = (NodeList) node.getChildNodes();
                for (int j = 0; j < nodes1.getLength(); j++) {
                    final Node node1 = (Node) nodes1.item(j);
                    System.out.println("Node  Child " + node1.getNodeName()
                            + " value : =" + node1.getTextContent());
                }

            }
        }

这是我的输出:-

节点名称配置有子节点子#text value : =

节点子文件上传值:=${project.build.directory}/${project.artifactId}-${project.version}.apk 节点子#text 值:=

子节点#注释值:= b3b47266-2d07-44d2-a4c4-23a64a2c9ff8 e707d458-c9ae-4ce5-887f-8c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba 子节点#文本值:=

子节点secretKey值:=b3b47266-211111111d07-44d2-a41111111c4-23a64a2c9ff8 子节点#text值:=

子节点 uploadKey 值:=e707d458-c9ae-4ce111111111115-887f-8111111111111111c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba 子节点#文本值:=

Node Child releaseNote value : =WLP_TEST from Maven Node Child #text value : =

节点子 grantUsers 值:= c273ae88-2314-4d85-a7e5-2c2cd40638a8 22a8d3df-7724-4941-8bd4-a735a807bc99

节点子#text值:=

我不确定为什么要打印 #text 节点,以及我们是否可以逃脱它。

4

1 回答 1

0

getChildNodes() 返回所有节点,包括文本节点(这很明显)。因此,您必须使用过滤掉文本节点if (node1.nodeType == 1)(但该值必须有一个常量1,请查阅您的语言/库手册以找出名称)。

于 2013-11-16T00:00:09.187 回答