
July 3rd, 2004, 03:23 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 22,309
Time spent in forums: < 1 sec
Reputation Power: 24
|
|
|
Parsing XML with PHP
Thanks a lot for the very helpful XML Parser Tutorial. Unfortunately I've got some small problems with it. I have a simple XML document:
php Code:
Original
- php Code |
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?> <player> <person> <lname>Smith</name> <fname>John</vorname> <email>john@smith.com</email> </person> <person> <lname>Miller</name> <fname>Steven</vorname> <email>steven@miller.com</email> </person> </player>
Now I want to grab the content (first name, last name, email) from the XML document into PHP arrays and have the following code:
php Code:
Original
- php Code |
|
|
|
function opening_element($parser, $element) { // opening XML element callback function $element = 'person'; } function closing_element($parser, $element) { // closing XML element callback function $element = 'person'; } function character_data($parser, $data) { // callback function for character data $lastName[] = $data; $firstName[] = $data; $emailAddress[] = $data; } $document = file('players.xml'); foreach ($document as $line) { }
If I want to access the data, it is not stored in lastName[0], lastName[1], ..., but:
lastName[4] -> Smith
lastName[7] -> John
lastName[10] -> john@smith.com
How can I make it work, that the data is accurately stored in the right arrays? I would highly appreciate any help.
Martin
|