在PHP中显示XML数据

时间:2010-01-27 11:47:07

标签: php xml

我的XML输出看起来像这样......

如何在PHP中显示它?

  <row>
        <field name="seat_no">18</field>
        <field name="my_ticket_no">924403</field>
        <field name="pass_nm">abcd</field>
        <field name="pass_age">46</field>
        <field name="pass_sex">F</field>
  </row>

  <row>
        <field name="seat_no">19</field>
        <field name="my_ticket_no">926634</field>
        <field name="pass_nm">VANDANA</field>
        <field name="pass_age">25</field>
        <field name="pass_sex">F</field>
  </row>

1 个答案:

答案 0 :(得分:2)

<?php
$data = new SimpleXMLElement('<root><row>
        <field name="seat_no">18</field>
        <field name="my_ticket_no">924403</field>
        <field name="pass_nm">abcd</field>
        <field name="pass_age">46</field>
        <field name="pass_sex">F</field>
  </row>

  <row>
        <field name="seat_no">19</field>
        <field name="my_ticket_no">926634</field>
        <field name="pass_nm">VANDANA</field>
        <field name="pass_age">25</field>
        <field name="pass_sex">F</field>
  </row></root>');
echo "<table border='1'>";
foreach($data->row as $row)
{
    echo "<tr>";
    $seat = $row->xpath("field[@name = 'seat_no']");
    echo "<td>".$seat[0]."</td>";
    $tckt = $row->xpath("field[@name = 'my_ticket_no']");
    echo "<td>".$tckt[0]."</td>";
    //and so on...
    echo "</tr>";
}  
echo "</table>";
?>