Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse xml inside xml object(with attribute) into array in cakephp

Tags:

cakephp

I have a response like this:

<test xmlns="http://schemas.datacontract.org/2004/07/test.Messages.test" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ProcessInstanceId xmlns="http://schemas.test.org/2004/07/test.Messages.Core">047d11f9-e381-4134-8bfa-da94cefda316</ProcessInstanceId>
    <ResultData i:type="a:InfoByZipCode" xmlns="http://schemas.test.org/2004/07/test.Messages.Core" xmlns:a="http://schemas.test.org/2004/07/test.DomainModel.Transient">
        <a:City>Bangalore</a:City>
        <a:County>India</a:County>
        <a:Products xmlns:b="http://schemas.test.org/2004/07/test.DomainModel.Views"/>
        <a:State>Connecticut</a:State>
        <a:StateCode>SBC</a:StateCode>
        <a:ZipCode>56001</a:ZipCode>
    </ResultData>
    <Success>TRUE</Success>
    <ResponseTime>2013-06-20T07:30:09.7558017-04:00</ResponseTime>
</test>

How can I convert this into array format?

like image 919
Ayesha Avatar asked Oct 03 '22 18:10

Ayesha


1 Answers

As far I know, everything you want to know you can find in the Cakebook: http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html

So you can parse XML into array as simple as:

$xmlString = '<?xml version="1.0"?><root><child>value</child></root>'; // XML string
$xmlArray = Xml::toArray(Xml::build($xmlString));

In the result your $xmlArray will have something like this:

Array
(
    [root] => Array
       (
           [child] => value
       )
 )
like image 168
Ziemo Avatar answered Oct 07 '22 23:10

Ziemo