2013-06-20 17 views
5

陣列I有一個這樣的響應:解析XML的XML對象(屬性)內部分隔成在CakePHP中

<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> 

我怎樣才能轉換到這個陣列格式?

回答

4

據我所知,你想要的一切,知道你可以在Cakebook發現:http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html

所以,你可以解析XML到數組一樣簡單:

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

在結果你$xmlArray會有這樣的事情:

Array 
(
    [root] => Array 
     (
      [child] => value 
     ) 
) 
+0

良好的工作ziemo ... – Pank

+0

感謝ziemo,而加入這一行太(應用::進口( '實用', 'XML');)如果烏拉圭回合使用CakePHP 2。 X – Ayesha