Reputation: 17
I am trying to parse SOAP/XML response with PowerShell. How to I setup the namespace for the request and response? How do I parse through the SOAP Envelope?
I believe that I'm not understanding how to manage the namespace and properly deal with the SOAP Envelope. I am working with an API that provides the format of a request and response:
Here is the template for the SOAP 1.2 request:
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>
Here is the template for the SOAP 1.2 response:
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 parsing returned that entire string only.The sting returned in string looks like:
<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>
In PowerShell, how can I get each "ID" element into an array of strings.:
<?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>
How to I setup the namespace for the request and response? How do I parse through the SOAP Envelope?
Upvotes: 0
Views: 5404
Reputation: 36332
You can use the GetElementsByTagName method to get the array of ID nodes.
[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'
That would set $IDs
to:
298343
192723
298343
192723
Edit: Ok, if that doesn't work let's work with the string that was kicked back. This one:
<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>
You just want the ID numbers? Set the string to a variable, let's say $IDString
. Then we run that against a regex search, and from each match we select the value of the matched text.
$IDs = ([regex]"<ID>(.+?)</ID>").matches($IDString)|%{$_.Groups[1].value}
Now $IDs
now contains the 4 ID numbers as above.
Upvotes: 0