簡體   English   中英

使用PHP解析帶有遞增標記值的XML

[英]Parse XML with incresing tag values using PHP

我正在使用一些API,該API以XML格式返回用戶完成的交易的列表。 這就是它的樣子。

<Response>
<Status>00</Status>
<STMT>
   <T0>
      <ID>25624</ID>
      <DATE>30 JUNE 2014</DATE>
       <Amount>1500</Amount>
   </T0>
   <T1>
      <ID>11495</ID>
      <DATE>29 JUNE 2014</DATE>
       <Amount>1000</Amount>
   </T1>
   <T2>
      ----
      ----
      ---- 
</STMT>
<Bal>55</Bal>
</Response>

現在,我們如何在STMT標簽中獲取這些值? 我試過了,但是沒用。

$result=simplexml_load_string($xmlstring);
$i='0';
$tx ='T'.$i;
while ($result->STMT->$tx) {
    $result->STMT->$tx->ID;
    $tx='T'.strval(intval($i++));
}

請幫忙。

實際上,這很簡單。 做這樣的事情:

$xml_string = '<Response><Status>00</Status><STMT> <T0> <ID>25624</ID> <DATE>30 JUNE 2014</DATE> <Amount>1500</Amount> </T0> <T1> <ID>11495</ID> <DATE>29 JUNE 2014</DATE> <Amount>1000</Amount> </T1> <T2> <ID>11496</ID> <DATE>28 JUNE 2014</DATE> <Amount>500</Amount> </T2></STMT><Bal>55</Bal></Response>';
$xml = simplexml_load_string($xml_string);
$stmt = $xml->STMT;
$stmt = json_decode(json_encode($stmt), true);
echo '<pre>';
print_r($stmt);

輸出:

Array
(
    [T0] => Array
        (
            [ID] => 25624
            [DATE] => 30 JUNE 2014
            [Amount] => 1500
        )

    [T1] => Array
        (
            [ID] => 11495
            [DATE] => 29 JUNE 2014
            [Amount] => 1000
        )

    [T2] => Array
        (
            [ID] => 11496
            [DATE] => 28 JUNE 2014
            [Amount] => 500
        )

)

有用。 你沒有回應結果...

echo $result->STMT->$tx->ID . "\n";

此外, $i++應該更改為++$i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM