使用simplexml_load_string用php解析xml

时间:2013-12-10 18:28:38

标签: php xml parsing

我有以下XML结果simplexml_load_string()...

SimpleXMLElement Object ( 
[Email] => xxxx@xxx.com 
[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子句中记录所有元素,如Trx_Id,Trx_date等......

任何人都可以发布代码吗?

我尝试过使用

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

但没有......!

更新:!!!!

使用以下方法

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>');

}    

我可以获得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 'xxxx@xxxx.com' (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浏览器

直接调用的Pure XML
This XML file does not appear to have any style information associated with it. The document tree       
is shown below.
<Report>
<Email>xxxxx@dddddd.com</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>

1 个答案:

答案 0 :(得分:0)

未经测试:

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