我在尝试使用Xpath解析XML内容时遇到了困难。 Xml包含名称空间信息。我曾尝试创建一个NameSpaceContextImp(jdk中的NameSpaceContext接口的apache WS commons实现)来将namaspace前缀映射到URI,但是无法成功查询xml文档。当我在http://chris.photobooks.com/xml/default.htm使用在线xpath测试工具时,我使用的xpath查询会出现我预期的节点/元素。所以我想弄清楚我做错了什么。我提供了xml文档和示例代码段。我将不胜感激任何反馈。作为一个注释,我尝试过使用和不使用名称空间前缀的xpath查询。
NamespaceContextImpl namespaceContext = new NamespaceContextImpl();
namespaceContext.startPrefixMapping("wsp", "http://schemas.xmlsoap.org/ws/2002/12/policy");
namespaceContext.startPrefixMapping("L7p", "http://www.layer7tech.com/ws/policy");
String policyXml = "xml content that is pasted below"
InputSource inputSource = new InputSource(new StringReader(policyXml));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xPath = xpathFactory.newXPath();
xPath.setNamespaceContext(namespaceContext);
XPathExpression xpathExpression = xPath.compile("/wsp:Policy/wsp:All");
String evaluation = xpathExpression.evaluate(inputSource);
if (evaluation.trim().length() > 0) {
System.out.println(evaluation);
}
<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
<wsp:All wsp:Usage="Required">
<L7p:SetVariable>
<L7p:AssertionComment assertionComment="included">
<L7p:Properties mapValue="included">
<L7p:entry>
<L7p:key stringValue="RIGHT.COMMENT"/>
<L7p:value stringValue="Used to enable message logging, Null (allow MSGDEBUG Header to set level), 0 - default,1 - Medium,2 - Full"/>
</L7p:entry>
</L7p:Properties>
</L7p:AssertionComment>
<L7p:Base64Expression stringValue=""/>
<L7p:VariableToSet stringValue="LOCAL_POLICY_DEBUG_LEVEL"/>
</L7p:SetVariable>
<L7p:Include>
<L7p:PolicyGuid stringValue="ec1f4166-4299-4e44-bf9d-c5c2a9f0c894"/>
</L7p:Include>
<L7p:SslAssertion>
<L7p:Option optionValue="Optional"/>
</L7p:SslAssertion>
<wsp:OneOrMore L7p:Enabled="false" wsp:Usage="Required">
<L7p:SpecificUser>
<L7p:Enabled booleanValue="false"/>
<L7p:IdentityProviderOid longValue="-2"/>
<L7p:UserLogin stringValue="test"/>
<L7p:UserName stringValue="test"/>
<L7p:UserUid stringValue="58916874"/>
</L7p:SpecificUser>
<L7p:SpecificUser>
<L7p:Enabled booleanValue="false"/>
<L7p:IdentityProviderOid longValue="-2"/>
<L7p:UserLogin stringValue="test"/>
<L7p:UserName stringValue="test"/>
<L7p:UserUid stringValue="58916873"/>
</L7p:SpecificUser>
<L7p:SpecificUser>
<L7p:Enabled booleanValue="false"/>
<L7p:IdentityProviderOid longValue="-2"/>
<L7p:UserLogin stringValue="test"/>
<L7p:UserName stringValue="test"/>
<L7p:UserUid stringValue="58916876"/>
</L7p:SpecificUser>
<L7p:SpecificUser>
<L7p:Enabled booleanValue="false"/>
<L7p:IdentityProviderOid longValue="-2"/>
<L7p:UserLogin stringValue="test"/>
<L7p:UserName stringValue="test"/>
<L7p:UserUid stringValue="58916875"/>
</L7p:SpecificUser>
<L7p:SpecificUser>
<L7p:Enabled booleanValue="false"/>
<L7p:IdentityProviderOid longValue="-2"/>
<L7p:UserLogin stringValue="testengineering-user"/>
<L7p:UserName stringValue="testengineering-user"/>
<L7p:UserUid stringValue="48201728"/>
</L7p:SpecificUser>
</wsp:OneOrMore>
<wsp:OneOrMore wsp:Usage="Required">
<L7p:HttpRoutingAssertion>
<L7p:ProtectedServiceUrl stringValue="http://localhost:13000/Services/Finance/v1"/>
<L7p:RequestHeaderRules httpPassthroughRuleSet="included">
<L7p:Rules httpPassthroughRules="included">
<L7p:item httpPassthroughRule="included">
<L7p:Name stringValue="Cookie"/>
</L7p:item>
<L7p:item httpPassthroughRule="included">
<L7p:Name stringValue="SOAPAction"/>
</L7p:item>
</L7p:Rules>
</L7p:RequestHeaderRules>
<L7p:RequestParamRules httpPassthroughRuleSet="included">
<L7p:ForwardAll booleanValue="true"/>
<L7p:Rules httpPassthroughRules="included"/>
</L7p:RequestParamRules>
<L7p:ResponseHeaderRules httpPassthroughRuleSet="included">
<L7p:Rules httpPassthroughRules="included">
<L7p:item httpPassthroughRule="included">
<L7p:Name stringValue="Set-Cookie"/>
</L7p:item>
</L7p:Rules>
</L7p:ResponseHeaderRules>
</L7p:HttpRoutingAssertion>
<L7p:Include>
<L7p:PolicyGuid stringValue="b438384e-eeb0-45c5-8a7e-d30da78f07ee"/>
</L7p:Include>
</wsp:OneOrMore>
</wsp:All>
</wsp:Policy>
答案 0 :(得分:1)
All
元素下唯一的CDATA是空格,您可以修剪它。如果您想获取DOM元素,请使用NODE或NODESET(NodeList
)选项。
String xml = "<foo><bar baz='hello' /></foo>";
InputSource src = new InputSource(new StringReader(xml));
XPathExpression expr = XPathFactory.newInstance().newXPath()
.compile("/foo/bar");
Node node = (Node) expr.evaluate(src, XPathConstants.NODE);
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.transform(new DOMSource(node), new StreamResult(System.out));
答案 1 :(得分:0)
使用一个参数调用evaluate()请求将结果转换为字符串,这相当于获取所请求节点的字符串值:即所选元素的文本节点后代的串联。由于所有后代文本节点都是空格,因此您将无法获得任何内容。
一次,名称空间不是问题。