0

I am getting xml back from an API request and want to know how to turn only the <soap:Body> to </soap:Body> section of it into an array in PHP. I haven't seen any simple or logical ways to do this in my searching.

Any information that would point me in the correct direction would be great!

Thanks in advance.

Sample XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Header>
    <wsa:Action>http://www.urlhere.com/</wsa:Action>
    <wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>
    <wsa:RelatesTo>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:RelatesTo>
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
    <wsse:Security>
      <wsu:Timestamp wsu:Id="Timestamp-4cf61b60-957a-454a-a828-90bd373301af">
        <wsu:Created>2001-01-01T00:00:00Z</wsu:Created>
        <wsu:Expires>2001-01-01T00:00:00Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <blah xmlns="http://www.urlhere.com/schema">
      <blahblah>
        <blahblahblah>0</blahblahblah>
      </blahblah>
    </blah>
  </soap:Body>
</soap:Envelope>

I would like this as a result

array('body'=>array('blah'=>array('blahblah'=>array('blahblahblah'=>0))))
3
  • $array=json_decode(json_encode(simplexml_load_string($xmlstring)),true); print_r($array); Should help Commented Apr 23, 2014 at 23:27
  • 1
    I'd say you should be using the PHP Soap Client for that API Commented Apr 23, 2014 at 23:28
  • @user574632 that gives me a blank array back. Commented Apr 23, 2014 at 23:33

1 Answer 1

1

If you were to use SimpleXML http://www.php.net/manual/en/book.simplexml.php

You could craft an xpath to match on the namespaced element.

$simple = simplexml_load_string($xml);
$result = $simple->xpath('//soap:Body');
echo $result[0]->blah->blahblah->blahblahblah;

I'm not going to present the "array" solution as complex XML structures require different kinds of processing/iteration, all covered by the simpleXML documentation.


Addition: Adding a better example of namespace consumption

$simple = simplexml_load_string($xml);
$simple->registerXPathNamespace('foo','http://www.urlhere.com/schema');
$result = $simple->xpath('//foo:blah');
echo $result[0]->blahblah->blahblahblah;
Sign up to request clarification or add additional context in comments.

1 Comment

These are going to be dynamic complex XML responses. Each containing different xml tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.