1

I have question , how to parsing data from multilevel tag colon XML file with dom PHP. Below is my XML sample data. I want to get data inside < transfer > and return as an array data

<soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <HostCustomerResponse xmlns="http://xx.xx.xx.xx">
      <ns1:output xmlns:ns1="http://xx.xx.xx" xmlns:ns2="some:url" xsi:type="ns2:Output">
       <ns2:statusCode>00</ns2:statusCode>
       <ns2:statusMsg/>
       <ns2:txnResponseDateTime>20190625164236</ns2:txnResponseDateTime>
       <ns2:txnData>
         <transferRequest>
           <transfer>
             <transferId>123456789</transferId>
             <txnDate>123456789</txnDate>
             <debitAcctNo>123456789</debitAcctNo>
             <benAcctNo>123456789</benAcctNo>
          </transfer>
        </transferRequest>
      </ns2:txnData>
    </ns1:output>
   </HostCustomerResponse>
  </soapenv:Body>
</soapenv:Envelope>

and this result i want.

array(
  [transferID] => 123456789,
  [txnDate] => 123456789, 
  .....
)

2 Answers2

1

Not sure if http://xx.xx.xx.xx is the real namespace in

<HostCustomerResponse xmlns="http://xx.xx.xx.xx">

but as this defines the namespace for any default elements (i.e. the ones your after) then you need to load the source XML and then register this namespace. Then you can use XPath to find the <transfer> element. You then just iterate through the elements within that ( using children() and add them into the output array...

$xml = simplexml_load_string($source);
$xml->registerXPathNamespace("d", "http://xx.xx.xx.xx");
$transfer = $xml->xpath("//d:transfer")[0];
$output = [];
foreach ( $transfer->children() as $key=> $value )  {
    $output[$key] = (string)$value;
}

Your original XML is missing the definition of the soapenv namespace, so I added that to make the XML correct...

<soapenv:Envelope xmlns:soapenv="http://soapev.com" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <HostCustomerResponse xmlns="http://xx.xx.xx.xx">
      <ns1:output xmlns:ns1="http://xx.xx.xx" xmlns:ns2="some:url" xsi:type="ns2:Output">
       <ns2:statusCode>00</ns2:statusCode>
       <ns2:statusMsg/>
       <ns2:txnResponseDateTime>20190625164236</ns2:txnResponseDateTime>
       <ns2:txnData>
         <transferRequest>
           <transfer>
             <transferId>123456789</transferId>
             <txnDate>123456789</txnDate>
             <debitAcctNo>123456789</debitAcctNo>
             <benAcctNo>123456789</benAcctNo>
          </transfer>
        </transferRequest>
      </ns2:txnData>
    </ns1:output>
   </HostCustomerResponse>
  </soapenv:Body>
</soapenv:Envelope>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • It's work. Thank you so much ! But if I want to get data like value of , etc, what should I change from the code ? – Belly Brata Jun 26 '19 at 06:51
  • You can do a similar thing - register the ns2 namespace `$xml->registerXPathNamespace("ns2", "some:url");` and then `$xml->xpath("//ns2:statusMsg")[0]` etc. – Nigel Ren Jun 26 '19 at 06:56
0

You can get the transfer node by using the following code snippet

$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soapXMLResult);
$xml      = new SimpleXMLElement($response);
$body     = $xml->xpath('//soapenvBody')[0];
$array    = json_decode(json_encode((array)$body), TRUE); 
$transfer = $array['HostCustomerResponse']['ns1output']['ns2txnData']['transferRequest']['transfer'];

https://3v4l.org/UKnZs

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • Thanks. But when I try to another XML response, I get some error because the XML tag colon response dynamically change. Thanks dude btw – Belly Brata Jun 26 '19 at 07:01
  • @BellyBrata you can use array till index `$array['HostCustomerResponse']['ns1output']['ns2txnData']['transferRequest']` , may be that helps OR any other array index which is common in all the response. – Rakesh Jakhar Jun 26 '19 at 07:03