gpt4 book ai didi

java - XPathExpression Evaluate 在 java 中返回一个空 NodeList

转载 作者:行者123 更新时间:2023-12-02 10:43:41 24 4
gpt4 key购买 nike

我尝试从标签获取值属性:

<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.test.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.test.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.test.com/us/hlrp/topic/c64a80ee742330778d018b0505cda195.rdf" aspect="metadata" path="documents/c64a80ee742330778d018b0505cda195/meta.rdf"/>
</pip:object-group>

我在Java中获取值(value)的方法是:

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。您能帮忙吗?预先感谢您。

最佳答案

您需要告诉 DocumentBuilderFactory 解析 namespace :

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

关于java - XPathExpression Evaluate 在 java 中返回一个空 NodeList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52747098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com