【问题标题】:Extracting a term from SOAP header从 SOAP 标头中提取术语
【发布时间】:2012-09-04 04:45:40
【问题描述】:

我想从 SOAP 标头中提取一个名为 ServiceGroupID 的元素,它指定事务的会话。我需要这个,以便我可以使用 SOAP 会话将请求定向到同一服务器。我的 XML 如下:

<?xml version="1.0" encoding="http://schemas.xmlsoap.org/soap/envelope/" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/none</wsa:Address>
<wsa:ReferenceParameters>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:99A029EBBC70DBEB221347349722532</axis2:ServiceGroupId>
</wsa:ReferenceParameters>
</wsa:ReplyTo>
<wsa:MessageID>urn:uuid:99A029EBBC70DBEB221347349722564</wsa:MessageID>
<wsa:Action>Perform some action</wsa:Action>
<wsa:RelatesTo>urn:uuid:63AD67826AA44DAE8C1347349721356</wsa:RelatesTo>
</soapenv:Header>

我想知道如何使用 Xpath 从上述 XML 中提取 Session GroupId。

【问题讨论】:

    标签: java xml soap xpath soapheader


    【解决方案1】:

    您尚未指定技术,因此假设您尚未设置等效的 .NET 命名空间管理器或类似工具,您可以使用与命名空间无关的 Xpath,如下所示:

    /*[local-name()='Envelope']/*[local-name()='Header']
          /*[local-name()='ReplyTo']/*[local-name()='ReferenceParameters']
          /*[local-name()='ServiceGroupId']/text()
    

    编辑已针对 Java 更新

    没有命名空间别名

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expression = xpath.compile("/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='ReplyTo']/*[local-name()='ReferenceParameters']/*[local-name()='ServiceGroupId']/text()");
    System.out.println(expression.evaluate(myXml));
    

    NamespaceContext

    NamespaceContext context = new NamespaceContextMap(
        "soapenv", "http://schemas.xmlsoap.org/soap/envelope/", 
        "wsa", "http://www.w3.org/2005/08/addressing",
        "axis2", "http://ws.apache.org/namespaces/axis2");
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(context);
    XPathExpression expression = xpath.compile("/soapenv:Envelope/soapenv:Header/wsa:ReplyTo/wsa:ReferenceParameters/axis2:Serv‌​iceGroupId/text()");
    System.out.println(expression.evaluate(myXml));
    

    local-name() 给出与其命名空间无关的元素的标签名称。 此外,上述 xml 文档中的 encoding 看起来不正确。

    编辑

    假设urn:uuid: 是一个常量,下面的XPath 将去掉结果的前9 个字符(与上面的任何一个XPath 一起使用)。如果urn:uuid 不是常量,那么您需要标记化/拆分等,which is beyond my skills

    substring(string(/*[local-name()='Envelope']/*[local-name()='Header']
                     /*[local-name()='ReplyTo']/*[local-name()='ReferenceParameters']
                     /*[local-name()='ServiceGroupId']/text()), 10)
    

    【讨论】:

    • @Kishorepandey 以上内容适用于任何解析器 - 我的意思是,如果您设置了命名空间管理器,那么您将能够为命名空间设置别名,然后以更“友好”的方式访问元素方式,即/soapenv:Envelope/soapenv:Header/wsa:ReplyTo/wsa:ReferenceParameters/axis2:ServiceGroupId/text()
    • @nonnb 你能给我一个例子来读取我的 xml 文件并执行 xpath 表达式并显示结果吗?当我尝试您在上面发布的表达式时,它似乎不起作用。如果你能在这方面帮助我,我会很高兴!
    • @nonnb 嗨,伙计,我试过运行你的代码,效果很好。但问题是我得到解析输出为 urn:uuid:99A029EBBC70DBEB221347349722532 而我只想要 uuid: 之后的阶段,我不需要 urn:uuid: 我能做些什么来解决它?
    • @Kishorepandey 使用子字符串更新以刮掉前缀,假设它是常量。
    • @nonnb 我如何将它包含在您上面的 xpath.compile() 中没有命名空间的示例中?是的,它是一个常数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多