在XML响应中获取节点的值

时间:2015-09-14 20:35:49

标签: xml web-services powershell soap

我在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

有没有办法在响应中查看XML,就像我可以使用SoapUI一样?

我甚至不知道我得到的回答是否正确。

1 个答案:

答案 0 :(得分:1)

XML使用命名空间。有问题的命名空间是<urls>节点上定义的:

<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