devWhiz Posted February 27, 2011 Share Posted February 27, 2011 ok say I have a link called in file_get_contents and it returns xml and I want to have each <entry></entry> defined to a different variable like -<entry> <id>1234</id> <name>Name</id> <age>Age</age> </entry> -<entry> <id>1234</id> <name>Name</id> <age>Age</age> </entry> -<entry> <id>1234</id> <name>Name</id> <age>Age</age> </entry> -<entry> <id>1234</id> <name>Name</id> <age>Age</age> </entry> See how there are 4 entry sets.. how would I assign the entrys to a variable and parse out the different names for each variable? Like $Entry1->name $Entry2->age $Entry3->id $Entry4->other Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/ Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 you need to 'sort out' your xml first.... <?php $string=" <document> <entry> <id>1234</id> <name>Name</name> <age>Age</age> </entry> <entry> <id>1234</id> <name>Name</name> <age>Age</age> </entry> <entry> <id>1234</id> <name>Name</name> <age>Age</age> </entry> <entry> <id>1234</id> <name>Name</name> <age>Age</age> </entry> </document> "; if(!$xml = simplexml_load_string($string)){ $message.="Can't connect to xml"; }else{ $count=1; foreach ($xml->entry as $value){ $GLOBALS['Entry'.$count]=$value->id; echo $GLOBALS['Entry'.$count]."<br />\n"; $count++; $GLOBALS['Entry'.$count]=$value->name; echo $GLOBALS['Entry'.$count]."<br />\n"; $count++; $GLOBALS['Entry'.$count]=$value->age; echo $GLOBALS['Entry'.$count]."<br />\n"; $count++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180243 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 how would I parse it out if its coming from a URL? $string = file_get_contents('URL'); Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180244 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 if(!$xml = simplexml_load_file(URL HERE)){ Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180265 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6 Warning: simplexml_load_string() [function.simplexml-load-string]: http://mob-dynamic-lb4.mobsters09.com/mob/get_city_list?user_id=45045691&auth_ke in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6 Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 6 Notice: Undefined variable: message in C:\Program Files (x86)\EasyPHP-5.3.3.1\www\Prop.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180271 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 I think they are error messages? but why they concern 'simplexml-load-string' and not 'simplexml_load_file' as I suggested you use, I have no idea! Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180273 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 is there another way to do it? it works great with the string how you have it set up, but its not reading the link.. hmm Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180276 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 maybe it is because the xml dock on the url has more entries <outer> − <xml> − <undeveloped_lands> − <land> <id>200</id> − <image_url> http%3A%2F%2Fcdn0%2Emobwarsapp%2Ecom%2Frpg%5Fimages%2Fopensocial%2Fmob%2Fingame%2Fterritory%2Fpawn%5Fshop%2Egif </image_url> − <details> %3Cspan%20class%3D%27landName%27%20style%3D%27color%3A%23FFFFFF%3B%20font%2Dsize%3A18px%3B%20font%2Dweight%3Abold%3B%27%3E%20Pawn%20Shop%3C%2Fspan%3E%3Cbr%3E%3Cspan%20class%3D%27landIncome%27%20style%3D%27color%3A%23FFFFFF%27%3EIncome%3A%20%24200%2C000%3C%2Fspan%3E%3Cbr%3E </details> <num_owned>0</num_owned> <cost>1000000000</cost> <is_racket>true</is_racket> <favor_points>0</favor_points> <purchase_limit>5</purchase_limit> <name>Pawn%20Shop</name> <income>$200,000</income> − <enhancements> − <enhancement> <value>1</value> − <suffix> % chance of a spirited defense (1/2x damage) when attacked </suffix> <prefix>+</prefix> <type>ATTACK_CRITICAL_DEFENSE_PERCENT</type> </enhancement> </enhancements> </land> − <land> <id>0</id> − <image_url> http%3A%2F%2Fcdn0%2Emobwarsapp%2Ecom%2Frpg%5Fimages%2Fopensocial%2Fmob%2Fingame%2Fterritory%2Fbig%2Fempty%5Flot%2Egif </image_url> − <details> %3Cspan%20class%3D%27landName%27%20style%3D%27color%3A%23FFFFFF%3B%20font%2Dsize%3A18px%3B%20font%2Dweight%3Abold%3B%27%3E%20Vacant%20Lot%3C%2Fspan%3E%3Cbr%3E%3Cspan%20class%3D%27landIncome%27%20style%3D%27color%3A%23FFFFFF%27%3EIncome%3A%20%24100%3C%2Fspan%3E%3Cbr%3E </details> <num_owned>0</num_owned> <is_racket/> <cost>4500</cost> <favor_points>0</favor_points> <name>Vacant%20Lot</name> <income>$100</income> </land> Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180278 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 It is getting very different from your original question! foreach ($xml->outer->xml->undeveloped_lands->land as $value){ Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180283 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 still returns the same error, I dont think it will let me load a url.. Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180285 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 does the xml page validate? http://validator.w3.org/feed/ Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180288 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 hmm, no it does not, this is the link http://mob-dynamic-lb4.mobsters09.com/mob/get_city_list?user_id=530830355&auth_key=d7240dc8bf350a48feb61eb04694bd67d5bc0cb1 Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180292 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 If a page doesn't validate, you can't parse it with simplexml_load_file Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180298 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 is there another function we can use? Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180301 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 This feed does not validate. line 3, column 0: Undefined root element: outer Should not stop it loading successfully. Give me a few moments, i'll try and parse it! Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180302 Share on other sites More sharing options...
harristweed Posted February 27, 2011 Share Posted February 27, 2011 works for me! <?php if($xml = simplexml_load_file("http://mob-dynamic-lb4.mobsters09.com/mob/get_city_list?user_id=530830355&auth_key=d7240dc8bf350a48feb61eb04694bd67d5bc0cb1")){ echo"XML File Connected<br />\n"; ob_flush(); flush(); }else{ echo"Can't connect to feed"; exit; } foreach ($xml->xml->undeveloped_lands->land as $value){ $id=$value->id; echo"id=$id<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180305 Share on other sites More sharing options...
devWhiz Posted February 27, 2011 Author Share Posted February 27, 2011 awesome!! Thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/228989-xml-help/#findComment-1180311 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.