geny Posted June 17, 2009 Share Posted June 17, 2009 Hi , There is this link -> http://data.giub.uni-bonn.de/openrouteservice/php/DetermineRoute_rajan.php?Start=7.0892567,50.7265543&Via=&End=7.0986258,50.7323634&lang=en&distunit=YD&routepref=Fastest&avoidAreas=&useTMC=false&noMotorways=false&noTollways=false&instructions=true Where I want to extract 2 things (shown below in BOLD): <xls:Instruction>Drive half left on Kaiserstraße</xls:Instruction> <xls:distance value="284" uom="YD"/> So,far I tried : $url="url_just_shown_above"; $output = file_get_contents($url); $xml = simplexml_load_string($output); echo $xml->{'xls:RouteInstruction'}->{'xls:Instruction'}."<br />"; but it does not helps, for attribute,I used : $xml = simplexml_load_file("url_just_shown_above"); foreach($xml->{'xls:distance[5]'}->attributes() as $a => $b) { echo $a,'="',$b,"\"</br>"; } it shows nothing either. The var_dump($xml); shows : object(SimpleXMLElement)#1 (1) { ["@attributes"]=> array(1) { ["version"]=> string(3) "1.1" } } Please help me extracting the BOLDed text from the XML file mentioned above..What wrong am I doing ? Any kind of guidance or CODE Snippet will be of great help..Will greatly appreciate the same.. Thanks a lot ! Link to comment https://forums.phpfreaks.com/topic/162594-extracting-from-xml-using-php/ Share on other sites More sharing options...
rhodesa Posted June 17, 2009 Share Posted June 17, 2009 something like this? <?php $xml = simplexml_load_file($url); $ns = $xml->getNamespaces(); $routes = $xml->xpath('//xls:RouteInstruction'); foreach($routes as $route){ $instruction = $distance = null; foreach($route->children($ns['xls']) as $node){ switch($node->getName()){ case 'Instruction': $instruction = (string)$node; break; case 'distance': $attrs = $node->attributes(); $distance = $attrs['value']; break; } } print "$instruction for $distance\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/162594-extracting-from-xml-using-php/#findComment-858200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.