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:
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.