【问题标题】:Getting value for node in XML response to SOAP在对 SOAP 的 XML 响应中获取节点的值
【发布时间】:2015-12-10 23:11:45
【问题描述】:

我在 PowerShell 中发出这样的 SOAP 请求:

$uri = "https://secure.echosign.com/services/EchoSignDocumentService20?WSDL"

$sun = Invoke-WebRequest $uri -Method post -ContentType "text/xml"  -InFile .\getUrl.xml

“文件”如下所示:

<api:getDocumentUrls>
  <api:apiKey>redacted</api:apiKey>
  <api:documentKey>REDACTED</api:documentKey>
  <api:options>
    <dto1:attachSupportingDocuments>true</dto1:attachSupportingDocuments> 
    <dto1:combine>true</dto1:combine>
    <dto1:auditReport>true</dto1:auditReport>    
  </api:options>
</api:getDocumentUrls>

我没有收到任何错误,并且在 SoapUI 中测试的响应的 xml 如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <ns1:getDocumentUrlsResponse xmlns:ns1="http://api.echosign">
      <ns1:getDocumentUrlsResult>
        <errorCode xmlns="http://dto14.api.echosign">OK</errorCode>
        <errorMessage xsi:nil="true" xmlns="http://dto14.api.echosign"/>
        <success xmlns="http://dto14.api.echosign">true</success>
        <supportingDocumentUrls xsi:nil="true" xmlns="http://dto14.api.echosign"/>
        <urls xmlns="http://dto14.api.echosign">
          <DocumentUrl>
            <name>test.pdf</name>
            <url>https://smuszconsulting.echosign.com/document/cp/2AAABLblqZhAOjW1Dv6ig0pMc1PaF5BSHiCNNVAU6PEBmVizdpa9kFwkX5wQurgfu7P0atiSKop4*/document.pdf</url>
          </DocumentUrl>
        </urls>
      </ns1:getDocumentUrlsResult>
    </ns1:getDocumentUrlsResponse>
  </soap:Body>
</soap:Envelope>

我需要将该 URL DocumentUrl.url 放入一个变量中,以便将其写入数据库,但我不知道如何。

我试过了:

$newObj = $sun.getDocumentUrlsresponse.ChildNodes | % { $_.Name; $_.InnerText }

但这不起作用。该对象甚至没有被创建。

好的..我将代码更改为如下所示:

$uri = "https://secure.echosign.com/services/EchoSignDocumentService20?wsdl"

[xml]$sun = Invoke-WebRequest $uri -Method post -ContentType "text/xml"  -InFile .\getUrl.xml

$nsm = New-Object Xml.XmlNamespaceManager($sun.NameTable)
$nsm.AddNamespace('ns', 'http://dto14.api.echosign')
$url = $sun.SelectSingleNode('//ns:DocumentUrl/ns:url').innerText

现在我收到此错误:

Exception calling "SelectSingleNode" with "1" argument(s): "Namespace Manager
or XsltContext needed. This query has a prefix, variable, or user-defined
function."
At C:\Program Files\DCESS_InfoGatherer\testingGetUrl.ps1:53 char:1
+ $url = $sun.SelectSingleNode('//ns:DocumentUrl/ns:url').innerText
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

有没有办法像使用 SoapUI 一样查看响应中的 XML?

我什至不知道我得到的回复是否正确。

【问题讨论】:

  • 我的错。由于某种原因,我在实例化命名空间管理器后未能实际使用它。 $nsm 必须作为第二个参数传递给SelectSingleNode()。请参阅我的更新答案。

标签: xml web-services powershell soap


【解决方案1】:

XML 使用名称空间。有问题的命名空间是在&lt;urls&gt; 节点上定义的:

<urls xmlns="http://dto14.api.echosign">

嵌套节点使用这个命名空间作为它们的默认命名空间,所以你需要一个命名空间管理器来访问它们。

[xml]$xml = $xmlResponseText  # turn the XML text into an XML object

$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('ns', 'http://dto14.api.echosign')
$url = $xml.SelectSingleNode('//ns:DocumentUrl/ns:url', $nsm).innerText

【讨论】:

  • 我的第一反应是&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt; &lt;s:Body&gt;&lt;u:GetInfoResponse xmlns:u="urn:dslforum-org:service:WLANConfiguration:1"&gt;。我可以应用您的代码还是必须更改$nsm.addnamespace(..),因为我有u:.. 和xmlns:u。在python和xmltodict 中有效:xml =xmltodict.parse(response.content.lower(), dict_constructor=dict);env = xml['s:envelope']..
猜你喜欢
  • 2016-01-03
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多