2013-12-10 82 views
0

我有以下XML結果simplexml_load_string()...解析XML與使用PHP simplexml_load_string

SimpleXMLElement Object ( 
[Email] => [email protected] 
[Acount] => 123123123 [Pin] => SimpleXMLElement Object () 
[StartDate] => 20131204 
[EndDate] => 20131204 [XML] => 1 [State] => 1 [Pays] => SimpleXMLElement Object () 
[Collections] => SimpleXMLElement Object ( 
    [Collection] => Array ( 
     [0] => SimpleXMLElement Object ([@attributes] => Array ([Trx_id] => 6200106301) [Trx_Date] => 2013-12-04 07:44:04 [Trx_Payment] => 243.00 [Trx_MontoNeto] => 229.45 [Trx_Number] => 42183710 [Trx_PaymentMean] => Pago) 
     [1] => SimpleXMLElement Object ([@attributes] => Array ([Trx_id] => 6200081097) [Trx_Date] => 2013-12-04 07:43:58 [Trx_Payment] => 160.00 [Trx_MontoNeto] => 150.47 [Trx_Number] => 42183698 [Trx_PaymentMean] => Pago) 
     [2] => SimpleXMLElement Object ([@attributes] => Array ([Trx_id] => 6200173497) [Trx_Date] => 2013-12-04 07:43:25 [Trx_Payment] => 78.00 [Trx_MontoNeto] => 72.42 [Trx_Number] => 42183631 [Trx_PaymentMean] => Pago) 
) 
[Tickets] => SimpleXMLElement Object () [Receptions] => SimpleXMLElement Object () [Retreats] => SimpleXMLElement Object ([Retreat] => SimpleXMLElement Object ([Trx_Date] => 2013-12-04 14:04:36 [Trx_Payment] => 0,000.17 [Trx_MontoNeto] => 0,000.80 [Trx_Number] => 56757 [Trx_RetreatMethod] => bla bla bla [Trx_TypeCodIdentif] => C.U.I.L. [Trx_NumberCodIdentif] => 234234234 [Trx_Bank] => bank. [Trx_Address] => SimpleXMLElement Object () [City] => SimpleXMLElement Object () [Province] => SimpleXMLElement Object ())) [Credits] => SimpleXMLElement Object () [Debits] => SimpleXMLElement Object ()) 

我想在foreach子句recorver像Trx_Id,Trx_date等所有元素...

誰能郵編做到這一點?

我使用

foreach ($xml->object as $dato) { 
    print_r('data ' . $dato->field[2]); 
} 

,但沒有嘗試過...!

UPDATE:!!!!

使用以下方法

foreach($xml->Collections->Collection as $result){ 
    print_r('id ' . $result['Trx_id']. '<br>'); 
    print_r('date ' . $result['Trx_Date']. '<br>'); 
    print_r('Trx_Payment ' . $result['Trx_Payment']. '<br>'); 
    print_r('Trx_Number ' . $result['Trx_Number']. '<br>'); 
    print_r('Trx_PaymentMean ' . $result['Trx_PaymentMean']. '<br>'); 
    print_r('......<br>'); 

}  

我可以olny得到Trx_id

,你可以看到

id 6200106301 
date 
Trx_Payment 
Trx_Number 
Trx_PaymentMean 

請參加護理結構

[Collections] => SimpleXMLElement Object ( 
    [Collection] => Array ( 
     [0] => SimpleXMLElement Object ([@attributes] => Array ([Trx_id] => 6200106301) 
      [Trx_Date] => 2013-12-04 07:44:04 [Trx_Payment] => 243.00 
      [Trx_MontoNeto] => 229.45 
      [Trx_Number] => 42183710 
      [Trx_PaymentMean] => Pago) 

使用vardump !! ...

object(SimpleXMLElement)[1] 
    public 'Email' => string '[email protected]' (length=24) 
    public 'Acount' => string 'xxxxx' (length=7) 
    public 'Pin' => 
    object(SimpleXMLElement)[2] 
    public 'StartDate' => string '20131204' (length=8) 
    public 'EndDate' => string '20131204' (length=8) 
    public 'XML' => string '1' (length=1) 
    public 'State' => string '1' (length=1) 
    public 'Pays' => 
    object(SimpleXMLElement)[3] 
    public 'Collections' => 
    object(SimpleXMLElement)[4] 
     public 'Collection' => 
     array (size=13) 
      0 => 
      object(SimpleXMLElement)[10] 
       ... 
      1 => 
      object(SimpleXMLElement)[11] 
       ... 
      2 => 

更新2:

這裏直接使用Web瀏覽器

This XML file does not appear to have any style information associated with it. The document tree  
is shown below. 
<Report> 
<Email>[email protected]</Email> 
<Acount>234234234</Acount> 
<Pin/> 
<StartDate>20131204</StartDate> 
<EndDate>20131204</EndDate> 
<XML>1</XML> 
<State>1</State> 
<Pays/> 
<Collections> 
<Collection Trx_id="123123132"> 
<Trx_Date>2013-12-04 07:44:04</Trx_Date> 
<Trx_Payment>444.00</Trx_Payment> 
<Trx_MontoNeto>444.45</Trx_MontoNeto> 
<Trx_Number>333333</Trx_Number> 
<Trx_PaymentMean>blabla</Trx_PaymentMean> 
</Collection> 
<Collection Trx_id="34534345"> 
<Trx_Date>2013-12-04 07:43:58</Trx_Date> 
<Trx_Payment>160.00</Trx_Payment> 
<Trx_MontoNeto>150.47</Trx_MontoNeto> 
<Trx_Number>666666</Trx_Number> 
<Trx_PaymentMean>blabla2</Trx_PaymentMean> 
</Collection> 
+0

'$ dato-> Email'代替'$ dato->字段[2]' –

+0

$ dato-> Collections->集合[0] - > Trx_id,即會讓你開始,var_dump它,當你走。 –

+0

我只是用一個註釋更新帖子...罪我可以讓一步,但我不能讓工作! – Ppp

回答

0

未測試稱爲純XML:

$collections = $xml->xpath("//Collection"); 
foreach($collection as $collection) { 
    $id = $collection['Trx_id']; 
    $date = $collection->Trx_Date . ''; 
    $payment = $collection->Trx_PaymentMean . ''; 
    ... 
} 
+0

是的......但它似乎是Trx_Id它只有一個位置的數組?我無法弄清楚它是如何構建的以及如何輕鬆訪問數據。 – Ppp

+0

@Ppp給你的XML以及 – Samy

+0

薩米請了更新2,我張貼純XML! – Ppp