在对 SOAP 的 XML 响应中获取节点的值 [英] Getting value for node in XML response to SOAP
问题描述
我正在 PowerShell 中发出这样的 SOAP 请求:
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
文件"如下所示:
<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 如下所示:
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>
我需要将该 URL DocumentUrl.url
放入一个变量中,以便将其写入数据库,但我不知道如何.
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.
我试过了:
$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
现在我收到此错误:
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?
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.
推荐答案
XML 使用命名空间.有问题的命名空间是在
节点上定义的:
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
这篇关于在对 SOAP 的 XML 响应中获取节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!