XPathExpression Evaluate在Java中返回一个空的NodeList

时间:2018-10-10 19:01:03

标签: java xpath nodelist

我试图从代码中获取value属性:

<pip:property name="release-name" value="2018 November Release"/>

我的xml是:

<pip:object-group id="id-6b940c81-ed37-45ec-ac85-68ae13a6c6a0" target-object-uri="http://doc.wolterskluwer.com/us/hlrp/topic/c64a80ee742330778d018b0505cda195">
    <pip:properties>
        <pip:property name="release-name" value="2018 November Release"/>
    </pip:properties>
    <pip:store id="id-93379bb2-e837-42b9-8316-aef393f1e034" object-uri="http://doc.wolterskluwer.com/us/hlrp/topic/c64a80ee742330778d018b0505cda195.htm" aspect="content" path="documents/c64a80ee742330778d018b0505cda195/c64a80ee742330778d018b0505cda195.htm"/>
    <pip:store id="id-99a5d375-2084-4f82-b30c-c932bb4bbb31" object-uri="http://doc.wolterskluwer.com/us/hlrp/topic/c64a80ee742330778d018b0505cda195.rdf" aspect="metadata" path="documents/c64a80ee742330778d018b0505cda195/meta.rdf"/>
</pip:object-group>

我在Java中获得价值的方法是:

private String getReleaseFolder(String manifestFolder, String procedureId) throws Exception {

    String releaseFolder = new String();
    XPathExpression expr;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = (Document) builder.parse(manifestFolder);
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    xpath.setNamespaceContext(new NamespaceContext() {    //minimalist
        public String getNamespaceURI(String prefix) {
            if (prefix.equals("pip")) {
                getLogger().info("I am in the first branch");
                return "http://schema.wolterskluwer.com/pci/interface-protocol-v4.0/";
            }
            if (prefix.equals("xsi")){
                getLogger().info("I am in the second branch");
                return "http://www.w3.org/2001/XMLSchema-instance";
            } else {
                getLogger().info("I am in the third branch");
                return null;
            }
        }
        public String getPrefix(String namespaceURI) {
            return null;
        }
        public Iterator<String> getPrefixes(String namespaceURI) {
            return null;
        }
    });

    expr = xpath.compile("//pip:properties[following-sibling::pip:store[contains(@path,\"" + procedureId +"\")]]/pip:property[@name=\"release-name\"]/@value");     
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

    if (nl.getLength() == 0) {
        String emailBody = CsbConstants.EMAILBODY_MANIFEST + procedureId + CsbConstants.EMAILBODY_MANIFEST_PART;
        getLogger().info(emailBody);
        // here we send an email

        // here we throw a new Exception
        Exception e = new Exception(emailBody);
        throw e;
    }
    releaseFolder = nl.item(0).getNodeValue();

    return releaseFolder;
}

但是,我的NodeList:nl.getLength()为0。请您提供某种帮助吗?预先谢谢你。

1 个答案:

答案 0 :(得分:0)

您需要告诉您的DocumentBuilderFactory解析名称空间:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);