attaboy Posted May 17, 2012 Share Posted May 17, 2012 PHP 5.3.8 XAMPP on Windows XP I have an XML file that manages the input to an mp3 player on this page http://www.jimslounge.com/segovia/ The structure looks like this <content> <auto_play>yes</auto_play> <loop>yes</loop> <volume>60</volume> <artist> <song_title><![CDATA[bach - Suite No. 3 - Allemande]]></song_title> <artist_name><![CDATA[Andre Segovia]]></artist_name> <image_path>load/images/segovia.jpg</image_path> <mp3_path>load/songs/Bach - Suite No. 3 (for solo cello, arr. Duarte)- Allemande.mp3</mp3_path> <url target="_blank" open="yes">http://en.wikipedia.org/wiki/Andr%C3%A9s_Segovia</url> </artist> <artist> ... with <artist> being the repeating element I want to be able to read and eventually write to If I run this $xml = simplexml_load_file("load.xml"); var_dump($xml); I get this object(SimpleXMLElement)#1 (4) { ["auto_play"]=> string(3) "yes" ["loop"]=> string(3) "yes" ["volume"]=> string(2) "60" ["artist"]=> array(22) { [0]=> object(SimpleXMLElement)#2 (5) { ["song_title"]=> object(SimpleXMLElement)#24 (0) { } ["artist_name"]=> object(SimpleXMLElement)#25 (0) { } ["image_path"]=> string(23) "load/images/segovia.jpg" ["mp3_path"]=> string(74) "load/songs/Bach - Suite No. 3 (for solo cello, arr. Duarte)- Allemande.mp3" ... which is overwhelming This cleans it up but doesn't give me the artist name and song name I'm looking for. $xml = simplexml_load_file("load.xml"); echo $xml->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } The Output: content auto_play: yes loop: yes volume: 60 artist: artist: artist: My experience with simplexml only goes back a couple of hours so I'm pretty inept at this point. Any help will be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/262651-need-help-reading-xml/ Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 When you loop through $xml->children(), you will get an object for each child. So you would have a separate object for auto_play, loop, volume, and artist. These elements then become the root. So to get the song titles, you would do: foreach($xml->children() as $child) { echo $child->song_title[0] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/262651-need-help-reading-xml/#findComment-1346198 Share on other sites More sharing options...
attaboy Posted May 17, 2012 Author Share Posted May 17, 2012 fantastic had no idea it would be so simple!! thanks again you're a genius! Quote Link to comment https://forums.phpfreaks.com/topic/262651-need-help-reading-xml/#findComment-1346200 Share on other sites More sharing options...
scootstah Posted May 17, 2012 Share Posted May 17, 2012 Well, it is called SimpleXML.... Quote Link to comment https://forums.phpfreaks.com/topic/262651-need-help-reading-xml/#findComment-1346202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.