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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.