Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old July 3rd, 2004, 03:23 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1.  
  2. <?xml version="1.0" encoding="iso-8859-1"?>
  3. <player>
  4.   <person>
  5.     <lname>Smith</name>
  6.     <fname>John</vorname>
  7.     <email>john@smith.com</email>
  8.   </person>
  9.   <person>
  10.     <lname>Miller</name>
  11.     <fname>Steven</vorname>
  12.     <email>steven@miller.com</email>
  13.   </person>
  14. </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
  1.  
  2. $lastName = array();
  3. $firstName = array();
  4. $emailAddress = array();
  5.  
  6. function opening_element($parser, $element) {
  7. // opening XML element callback function
  8.     $element = 'person';
  9. }
  10.  
  11. function closing_element($parser, $element) {
  12. // closing XML element callback function
  13.     $element = 'person';
  14. }
  15.  
  16. function character_data($parser, $data) {
  17. // callback function for character data
  18.  
  19.    global $lastName;
  20.    $lastName[] = $data
  21.    
  22.    global $firstName;
  23.    $firstName[] = $data;
  24.    
  25.    global $emailAddress;
  26.    $emailAddress[] = $data;
  27. }
  28.  
  29. $parser = xml_parser_create();
  30. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  31. xml_set_element_handler($parser, 'opening_element', 'closing_element');
  32. xml_set_character_data_handler($parser, 'character_data');
  33.  
  34. $document = file('players.xml');
  35.  
  36. foreach ($document as $line) {
  37.     xml_parse($parser, $line);
  38. }
  39.  
  40. xml_parser_free($parser);


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

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Parsing XML with PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway