XPath评估问题

时间:2013-07-15 09:05:37

标签: xpath

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

下面是我的xml。使用Xpath表达式 // plugin / artifactId [text()=“test-maven”] 我能够过滤元素,但我想让孩子们兄弟姐妹的元素,如“配置”和其中的孩子。我尝试以这种方式修改我的Xpath

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

任何人都可以帮我解决这个问题

<?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());
                }

            }
        }

这是我的ouyput: -

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

Node Child fileToUpload value:= $​​ {project.build.directory} / $ {project.artifactId} - $ {project.version} .apk Node Child #text value:=

Node Child #comment value:= b3b47266-2d07-44d2-a4c4-23a64a2c9ff8                                  e707d458-c9ae-4ce5-887f-8c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba Node Child #text value:=

节点子密钥值:= b3b47266-211111111d07-44d2-a41111111c4-23a64a2c9ff8 Node Child #text value:=

节点子上传键值:= e707d458-c9ae-4ce111111111115-887f-8111111111111111c70f97ad801-51334304-d180-4031-917f-a5c58768c7ba Node Child #text value:=

Node Child releaseNote value:=来自Maven的WLP_TEST Node Child #text value:=

Node Child grantUsers值:=                                     c273ae88-2314-4d85-a7e5-2c2cd40638a8                                     22a8d3df-7724-4941-8bd4-a735a807bc99

Node Child #text value:=

我不确定为什么#text节点会被打印出来,如果我们手杖逃脱了。

1 个答案:

答案 0 :(得分:0)

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