gpt4 book ai didi

java - 使用 jdk xpath 解析具有 namespace 的 xml 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:51 25 4
gpt4 key购买 nike

我在尝试使用 Xpath 解析 XML 内容时遇到困难。 Xml 包含命名空间信息。我尝试创建一个 NameSpaceContextImp (jdk 中 NameSpaceContext 接口(interface)的 apache WS commons 实现)来将 namaspace 前缀映射到 URI,但是无法成功查询 xml 文档。当我使用在线xpath测试工具http://chris.photobooks.com/xml/default.htm时,我使用的 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>

最佳答案

All 元素下唯一的 CDATA 是您修剪的空白。如果你想获取 DOM 元素,请使用 NODENODESET (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));

关于java - 使用 jdk xpath 解析具有 namespace 的 xml 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903942/

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