mykel241 Posted July 9, 2007 Share Posted July 9, 2007 If someone can help me to parse this file and put the results into an array it would be great. I need the value between the name tags put into an array so I can print it out via a foreach loop. I am looking for the simplest option available. Thank you. XML sample: <?xml version="1.0" encoding="UTF-8"?> <webservices ver="1.0"> <status code="1.0"/> <markets> <market weight="10"> <name>Boston</name> </market> <market weight="10"> <name>Washington DC</name> </market> Quote Link to comment https://forums.phpfreaks.com/topic/59163-help-with-parsing-xml-file-part-ii-need-to-place-value-of-names-into-an-array/ Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 http://www.phpfreaks.com/forums/index.php/topic,148829.0.html Given the code in that post I would hope you can figure it out. The code is there just needs modifying. We are not here to code for you. Quote Link to comment https://forums.phpfreaks.com/topic/59163-help-with-parsing-xml-file-part-ii-need-to-place-value-of-names-into-an-array/#findComment-293837 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 try <?php $file = '<?xml version="1.0" encoding="UTF-8"?> <webservices ver="1.0"> <status code="1.0"/> <markets> <market weight="10"> <name>Boston</name> </market> <market weight="10"> <name>Washington DC</name> </market>'; preg_match_all('/<name>(.+?)<\/name>/', $file, $m); $names = $m[1]; print_r($names); ?> Quote Link to comment https://forums.phpfreaks.com/topic/59163-help-with-parsing-xml-file-part-ii-need-to-place-value-of-names-into-an-array/#findComment-293838 Share on other sites More sharing options...
Barand Posted July 9, 2007 Share Posted July 9, 2007 or try simplexml <?php $str = '<webservices ver="1.0"> <status code="1.0"/> <markets> <market weight="10"> <name>Boston</name> </market> <market weight="10"> <name>Washington DC</name> </market> </markets> </webservices>'; $xml = simplexml_load_string($str); foreach ($xml->xpath('/webservices/markets/market/name') as $name) echo $name.'<br>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/59163-help-with-parsing-xml-file-part-ii-need-to-place-value-of-names-into-an-array/#findComment-293918 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.