【问题标题】:issue parsing xml with namespaces using jdk xpath使用 jdk xpath 解析带有命名空间的 xml
【发布时间】:2012-06-09 20:41:52
【问题描述】:

我在尝试使用 Xpath 解析 XML 内容时遇到了困难。 XML 包含命名空间信息。我试图创建一个 NameSpaceContextImp(jdk 中 NameSpaceContext 接口的 apache WS commons 实现)来将 namaspace 前缀映射到 URI,但是无法成功查询 xml 文档。当我在http://chris.photobooks.com/xml/default.htm 使用在线 xpath 测试工具时,我使用的 xpath 查询会出现我预期的节点/元素。所以我想弄清楚我做错了什么。我正在提供 xml 文档和示例代码 sn-p。我将不胜感激任何反馈。作为说明,我尝试了使用和不使用命名空间前缀的 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>

【问题讨论】:

    标签: java xml xpath namespaces


    【解决方案1】:

    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));
    

    【讨论】:

    • 谢谢。您对 CDATA 的评论让我开始工作。虽然尝试了太多东西,但我还简化了我的 xpath 查询,并且没有寻找我最初寻找的实际元素/属性。我还没有试过你的代码被剪断,也不太明白为什么我需要一个转换器。
    • @John - 转换器仅用于在演示代码中将 XML 打印到 System.out,因为 toString()Node 类型没有太大作用 - 请参阅 here。跨度>
    【解决方案2】:

    使用一个参数调用evaluate() 请求将结果转换为字符串,这相当于取所请求节点的字符串值:即所选元素的文本节点后代的串联。由于所有后代文本节点都是空白,因此您将一无所获。

    这一次,命名空间不是问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2011-01-05
      • 2016-05-14
      • 2010-09-12
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多