Getting value for node in XML response to SOAP

て烟熏妆下的殇ゞ 提交于 2019-12-20 07:36:09

问题


I'm making a SOAP request in PowerShell like this:

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

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

The "file" looks like this:

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

I'm not getting any errors and the xml of the response as tested in SoapUI looks like this:

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

I need to get that URL DocumentUrl.url into a variable so I can write it to a DB, but I can't figure out how.

I tried:

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

but that's not working. The object is not even created.

Ok ..I changed my code to look like this:

$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

Now I get this error:

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

Is there some way to see the XML in the response like I can using SoapUI?

I don't even really know if the response I'm getting is correct.


回答1:


The XML uses namespaces. The namespace in question is the defined on the <urls> node:

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

The nested nodes use this namespace as their default namespace, so you need a namespace manager to access them.

[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


来源:https://stackoverflow.com/questions/32573470/getting-value-for-node-in-xml-response-to-soap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!