thewood68 Posted January 27, 2008 Share Posted January 27, 2008 im attaching a sample xml file where i need to retrieve information regarding song artist and song name....this is a long list so there are lines 17 and 18: <key>Name</key><string>Rocking Chair</string> <key>Artist</key><string>PW Gopal</string> lines 48 and 49: <key>Name</key><string>There Goes My Life</string> <key>Artist</key><string>Kenny Chesney</string> etc... these lists could potentially contain 1000's of these brackets. all i want from each one of these is Song Rocking Chair Artist PW Gopal Song There Goes My Life Artist Kenny Chesney Thanks [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/ Share on other sites More sharing options...
ziv Posted January 27, 2008 Share Posted January 27, 2008 try this code: <?php $xml = simplexml_load_file('your_xml_file'); $list = $xml->xpath('//dict/dict/dict/string[1]'); var_dump($list); Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450316 Share on other sites More sharing options...
thewood68 Posted January 27, 2008 Author Share Posted January 27, 2008 thats definently the right direction... my output for that is. what would i run to filter all the extra out just to get the "PW Gopal"? array(4) { [0]=> object(SimpleXMLElement)#2 (1) { [0]=> string( "PW Gopal" } [1]=> object(SimpleXMLElement)#3 (1) { [0]=> string(13) "Kenny Chesney" } [2]=> object(SimpleXMLElement)#4 (1) { [0]=> string(11) "Andy Griggs" } [3]=> object(SimpleXMLElement)#5 (1) { [0]=> string(14) "Colbie Caillat" } } Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450321 Share on other sites More sharing options...
sasa Posted January 27, 2008 Share Posted January 27, 2008 try <?php $f = file_get_contents('Amped.txt'); preg_match_all('/<key>Name<\/key><string>([^<]+)<\/string>[\\n\\r\\t ]*<key>Artist<\/key><string>([^<]+)<\/string>/',$f,$a); $b = array_combine($a[2],$a[1]); print_r($b); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450328 Share on other sites More sharing options...
thewood68 Posted January 27, 2008 Author Share Posted January 27, 2008 that gives me Array ( [PW Gopal] => Rocking Chair [Kenny Chesney] => There Goes My Life [Andy Griggs] => She Thinks She Needs Me [Colbie Caillat] => Bubbly ) i can definently tell its almost there but im not sure how i would go about dynamically printing out Artist PW Gopal Song Rocking Chair Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450331 Share on other sites More sharing options...
sasa Posted January 27, 2008 Share Posted January 27, 2008 <?php $f = file_get_contents('Amped.txt'); preg_match_all('/<key>Name<\/key><string>([^<]+)<\/string>[\\n\\r\\t ]*<key>Artist<\/key><string>([^<]+)<\/string>/',$f,$a); //$b = array_combine($a[2],$a[1]); //print_r($b); foreach ($a[1] as $k => $song){ $artist = $a[2][$k]; echo 'Artist: ',$artist,"\n",'Song: ',$song,"\n\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450334 Share on other sites More sharing options...
thewood68 Posted January 27, 2008 Author Share Posted January 27, 2008 you guys are absolutely amazing. thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/88013-solved-retrieving-info-from-xml-file/#findComment-450336 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.