mykel241 Posted July 9, 2007 Share Posted July 9, 2007 Hello, I need to find all occurances of <root contentId=" in this xml file and then be able to access the value of the content id via an array as needed. Here is sample XML: <webservices ver="1.0"> <status code="1"/> <channel> <name>somename</name> <shortName>some shortname</shortName> <authors> <author id="17">Some author</author> </authors> </channel> <messageFlow> <title>Some Title</title> <link>http://www.somelink.com</link> <deliveryDate>2007-07-03 00:00:00.0</deliveryDate> <root contentId="291655"> <numComments>0</numComments> <text>Some text</text> <tags/> </root> </messageFlow> <messageFlow> <title>Some Title</title> <link>http://www.somelink.com</link> <deliveryDate>2007-07-03 00:00:00.0</deliveryDate> <root contentId="291656"> <numComments>0</numComments> <text>Some text</text> <tags/> </root> </messageFlow> If anyone can help I would appreciate it. I would prefer to use regular expressions instead on a xml parser. Michael Quote Link to comment Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 <?php $xml_file = file_get_contents('xmlfile.xml'); $root_split = split("</root>", $xml_file); foreach ($root_split as $root) { if (eregi("\">", $root)) { list($ids[]) = split("\">", $root); } } print_r($ids); ?> Also here is my shot at a regex version <?php $xml_file = file_get_contents('xmlfile.xml'); ereg('<root contentId=\"([0-9]*)\">', $xml_file, $matches); print_r($matches); ?> I am not very good with Regex, but hopefully the above is correct and works. Quote Link to comment Share on other sites More sharing options...
mykel241 Posted July 9, 2007 Author Share Posted July 9, 2007 Thank you, The Reg Exp version works but only finds the first occurance. I am hoping to find all and then place them into an array so I can access them as needed. Any help is appreciated. Michael Quote Link to comment Share on other sites More sharing options...
mykel241 Posted July 9, 2007 Author Share Posted July 9, 2007 Also if anyone has a better but simple solution I would be willing to see it. I appreciate all your help. Michael Quote Link to comment Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 <?php $xml_file = file_get_contents('xmlfile.xml'); while (true) { if (ereg("<root contentId=\"([0-9]*)\">", $xml_file, $matches)) { $ids[] = $matches[1]; // I think thats right $xml_file = str_replace("<root contentId=\"" . $matches[1] . "\">", "", $xml_file); }else { break; } } print_r($ids); ?> Unsure if that will work, but yea. I thought there was a way that it would return all the results. But hopefully the above works and runs somewhat fast. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 9, 2007 Share Posted July 9, 2007 preg_match_all Quote Link to comment 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.