Jump to content

XML Help


davieboy

Recommended Posts

HI All

im trying to follow the o' tutorials but coming up totally blank

 


if(!$data=simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=51.519812,-0.200774&destination=51.26246,-1.084024&sensor=false")){
    trigger_error('Error reading XML file',E_USER_ERROR);
}
echo 'Displaying contents of XML file...<br />';
foreach($data as $directions){
    echo 'Start: '.$directions->route->leg->step->start_location->lat.' <br>End: '.$directions->route->leg->step->end_location->lng.'<br>
HTML: '.$directions->route->leg->step->html_instructions.' <br>Distance: '.$directions->route->leg->step->distancee->text.'<br /><br>';
}

Results just wont show at all, i know its to do with the way im going through the xml fields, but cant see where

 

Can anyone help  me at all?

Link to comment
https://forums.phpfreaks.com/topic/247327-xml-help/
Share on other sites

Try this after

if(!$data=simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=51.519812,-0.200774&destination=51.26246,-1.084024&sensor=false")){    trigger_error('Error reading XML file',E_USER_ERROR);}

Use this...

$start = $data->route->leg->step->start_location->lat;
$end = $data->route->leg->step->end_location->lng;
$html = $data->route->leg->step->html_instructions;
$distance = $data->route->leg->step->distance->text;
echo "Start: ".$start."<br />";
echo "End: ".$end."<br />";
echo "HTML: ".$html."<br />";
echo "Distance: ".$distance."<br />";

Link to comment
https://forums.phpfreaks.com/topic/247327-xml-help/#findComment-1270285
Share on other sites

Let me add a little more.  I think what you are doing is trying to extract all of the "steps" in the XML.  Since there are many steps, you are trying to extract each step.  Try looking at the PHP example: http://www.php.net/manual/en/simplexmlelement.children.php

 

My example only takes the first child with the name of step.

Link to comment
https://forums.phpfreaks.com/topic/247327-xml-help/#findComment-1270291
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.