我如何用PHP解析这个XML,并用它生成新的PHP文件? [关闭]

My question is about parsing xml with php. Although I understand some basics of php, please take into account that my understanding is very limited.

This is an example of the XML structure that I’m working with:

<XML_DATA item=“Beatles”>
<People>
    <Person Firstname=“George” Lastname=“Harrison” Instrument=“Guitar”>Harrison, George</Person>
    <Person Firstname=“John” Lastname=“Lennon” Instrument=“Guitar”>Lennon, John</Person>
    <Person Firstname=“Paul” Lastname=“McCartney” Instrument=“Bass”>McCartney, Paul</Person>
    <Person Firstname=“Ringo” Lastname=“Starr” Instrument=“Drums”>Starr, Ringo</Person>
</People>
</XML_DATA>

This is what I need to do with this data:

  1. Populate each Person into a table on an index page, displaying their Firstname and Lastname.
  2. For each Person, a page would be generated. On that page, Firstname, Lastname, and Instrument would be displayed.
  3. On the table on the index page, each Person’s Firstname/Lastname would be wrapped in an anchor tag, linking to their corresponding page.

How would I go about doing this?

I appreciate your help, thank-you.

Here is how to parse your xml file :

    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->load('your/xml/file.xml');
    $datas = $doc->getElementsByTagName('Person');
    foreach ($datas as $data)
    {
        echo $data->getAttribute('Firstname'); //displays "George", then "John" and so on...
        echo $data->getAttribute('Lastname'); //displays "Harrison", then "Lennon" and so on...
        echo $data->getAttribute('Instrument'); // displays "Guitar", then "Guitar" and so on...
        echo $data->nodeValue //displays "Harrison, George", then "Lennon, John" and so on...
    }

Now you can store the datas in an array so you can manipulate them whenever you want.