逐节点遍历XML文件并在PHP中显示详细信息

时间:2019-05-21 08:10:48

标签: php xml

我想用PHP编写一个程序,该程序将遍历XML文件并显示节点内容详细信息,例如我具有此xml文件名data.xml

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book> 
</catalog>

PHP

<?php
$reader = new XMLReader();
$reader->open('data.xml');

while($reader->read("book")){
    //TODO loop though all the book nodes and print details of each book.
}

?>

请提供一种方法,该方法可以读取书节点,并允许我遍历书节点并在其中打印详细信息。

0 个答案:

没有答案