我正在尝试使用PowerShell解析SOAP / XML响应。 如何为请求和响应设置命名空间? 如何解析SOAP信封?
我相信我不了解如何管理命名空间并正确处理SOAP Envelope。我正在使用提供请求和响应格式的API:
以下是SOAP 1.2请求的模板:
POST /LinkPlusWebService/WsIncident.asmx HTTP/1.1
Host: server.domain.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindIncidents4Customer xmlns="http://www.techexcel.com/">
<LinkedSystemID>string</LinkedSystemID>
<LinkedProjectID>string</LinkedProjectID>
<lProjectID>int</lProjectID>
<lCustomerID>int</lCustomerID>
</FindIncidents4Customer>
</soap12:Body>
</soap12:Envelope>
以下是SOAP 1.2响应的模板:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindIncidents4CustomerResponse xmlns="http://www.techexcel.com/">
<FindIncidents4CustomerResult>string</FindIncidents4CustomerResult>
</FindIncidents4CustomerResponse>
</soap12:Body>
</soap12:Envelope>
XML解析仅返回整个字符串。字符串中返回的sting看起来像:
<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . </TITLE><ID>192723</ID><TITLE>Loreum Ipsum</TITLE><ID>298343</ID><TITLE>Thanks for help</TITLE><ID>192723</ID><TITLE>Strings are hard</TITLE>
在PowerShell中,我如何将每个“ID”元素放入一个字符串数组中。:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindIncidents4CustomerResponse xmlns="http://www.techexcel.com/">
<FindIncidents4CustomerResult>
<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . </TITLE><ID>192723</ID><TITLE>Loreum Ipsum</TITLE><ID>298343</ID><TITLE>Thanks for help</TITLE><ID>192723</ID><TITLE>Strings are hard</TITLE>
</FindIncidents4CustomerResult>
</FindIncidents4CustomerResponse>
</soap12:Body>
</soap12:Envelope>
如何为请求和响应设置命名空间? 如何解析SOAP信封?
答案 0 :(得分:0)
您可以使用GetElementsByTagName方法获取ID节点数组。
[xml]$XML = @"
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FindIncidents4CustomerResponse xmlns="http://www.techexcel.com/">
<FindIncidents4CustomerResult>
<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . </TITLE><ID>192723</ID><TITLE>Loreum Ipsum</TITLE><ID>298343</ID><TITLE>Thanks for help</TITLE><ID>192723</ID><TITLE>Strings are hard</TITLE>
</FindIncidents4CustomerResult>
</FindIncidents4CustomerResponse>
</soap12:Body>
</soap12:Envelope>
"@
$IDs = $xml.GetElementsByTagName("ID").'#text'
这会将$IDs
设置为:
298343
192723
298343
192723
编辑:好的,如果这不起作用,请使用被踢回的字符串。这一个:
<ID>298343</ID><TITLE>This is free text-that may contain / any character . . . </TITLE><ID>192723</ID><TITLE>Loreum Ipsum</TITLE><ID>298343</ID><TITLE>Thanks for help</TITLE><ID>192723</ID><TITLE>Strings are hard</TITLE>
您只需要身份证号码?将字符串设置为变量,让我们说$IDString
。然后我们针对正则表达式搜索运行它,并从每个匹配中选择匹配文本的值。
$IDs = ([regex]"<ID>(.+?)</ID>").matches($IDString)|%{$_.Groups[1].value}
现在$IDs
现在包含上面的4个ID号。