Link to home
Create AccountLog in
Avatar of john-formby
john-formbyFlag for Ghana

asked on

php xml simplexml_load_file

Hi,

I am passing the address values to the google maps geocode url and generating an xml page.  What I want to do is display the xml file as it is on my page.  The problem is that when I tried to use an iframe it said "This content cannot be displayed in a frame"  I know I could use a popup window to display the output, but would rather everything was contained in one page.  

So now I am trying to use simplexml_load_file to do this.  It returns the xml page, but in an array format.  Is there a way I can display the output as it would display normally?

Here is what I am using so far:

if(isset($_POST['generate'])) {
    $address = $_POST['address'];
 
    $url = $geolocurl.$address;
    $xml = simplexml_load_file($url);
    var_dump($xml);
}

Kind Regards,

John
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Try

$file = file_get_contents($url);
echo $file;

Cheers
Avatar of john-formby

ASKER

Hi Marcus,

Thanks for the reply.  I already tried that but it returns the xml output in a really long string (with no element tags).  I need it to display the xml file in the same format as it would be rendered directly to the browser.  I added nl2br to get the line breaks but how do I display the element tags?

Kind Regards,

John
Sorry, forgot to say you have to set correct MIME type before outputting:


$file = file_get_contents($url);
header('Content-type: text/xml');
echo $file;

Hi Marcus,

This is a page that already has HTML output and the XML is just going to be a small part of the page.  I cannot use the header function as nothing is displayed on the page with this.

Is it possible to load the XML page inside a div?

Kind Regards,

John
Yes , you can use a div but this will not allow you to use header.
Unfortunately, I've not time to test now so I cen only say 'try' :-(

Now I would try to use

if(isset($_POST['generate'])) {
    $address = $_POST['address'];
 
    $url = $geolocurl.$address;
    $xml = simplexml_load_file($url);
    echo $xml->asXML();
}
Hi Marcus,

I tried that but it still does not format the xml correctly.  I am beginning to wonder if this is even possible?

Kind Regards,

John
Surely it is possible to do what you want, but we need to see a more complete picture of the issues.

Please post a complete example, including all the code and the test data.  We need to know what values are in all of the variables (example: $url = $geolocurl.$address;).  Once we have everything in a working script, we will be able to show you some ways to format the data for your requirements.
Hi,
you could use this function to display your xml_string:

hope this helps !
function xml_highlight($s)
{       
    $s = htmlspecialchars($s);
    $s = preg_replace("#<([/]*?)(.*)([\s]*?)>#sU",
        "<font color=\"#0000FF\">&lt;\\1\\2\\3&gt;</font>",$s);
    $s = preg_replace("#&lt;([\?])(.*)([\?])&gt;#sU",
        "<font color=\"#800000\">&lt;\\1\\2\\3&gt;</font>",$s);
    $s = preg_replace("#&lt;([^\s\?/=])(.*)([\[\s/]|&gt;)#iU",
        "&lt;<font color=\"#808000\">\\1\\2</font>\\3",$s);
    $s = preg_replace("#&lt;([/])([^\s]*?)([\s\]]*?)&gt;#iU",
        "&lt;\\1<font color=\"#808000\">\\2</font>\\3&gt;",$s);
    $s = preg_replace("#([^\s]*?)\=(&quot;|')(.*)(&quot;|')#isU",
        "<font color=\"#800080\">\\1</font>=<font color=\"#FF00FF\">\\2\\3\\4</font>",$s);
    $s = preg_replace("#&lt;(.*)(\[)(.*)(\])&gt;#isU",
        "&lt;\\1<font color=\"#800080\">\\2\\3\\4</font>&gt;",$s);
    return nl2br($s);
}

Open in new window

Hi Ray,

Yes, I am sure it is possible, I am just getting frustrated as I have tried everything I can think of to get this working :-)

I would like to load the XML output (including all tags and data) in a div on my page.  Here is my code for the page so far:

<?php
include('global/isloggedin.php');
if(!isset($_SESSION['uactive'])) {
      session_start();
        header('Location: login.php?flag=2');
} else {

include('../global/dbconnect.php');
include('global/clean.php');

foreach($_POST as $key=>$value) {
      $$key = ValidateInput($value);
}

include('global/header1.php');
$sqltitle = mysql_query("SELECT stitle FROM tblsitedetails WHERE sID = '1'");
$rowtitle = mysql_fetch_array($sqltitle);
echo '<title>'.$rowtitle['stitle'].' - Admin</title>';
include('global/header2.php');
echo '</head>
<body>';
include('global/header3.php');
include('global/navbar.php');

echo '<div id="maincol"><hr class="top" /><div class="adminmenu">

<br /><hr class="top" /><br />
<h1>Generate Geocode Details</h1>

<form action="elocation.php" method="post">
    <div class="clearfix">
            <div class="col150"><label>Location</label></div>
            <div class="col550"><input class="txtinput" type="text" name="address" /></div>
      </div>

    <div class="clearfix">
            <div class="col150">&nbsp;</div>
            <div><input type="submit" name="generate" value="Generate" /></div>
      </div>
</form>';

if(isset($_POST['generate'])) {
    $address = $_POST['address'];

    /*$url = $geolocurl.$address;
    $xml = simplexml_load_file($url);
    echo $xml->asXML();*/

    //echo '<div style="width:600px;height:400px;">'.header('Location: '.$geolocurl.$address).'</div>';

}

echo '</div><hr class="top" /></div>';

include('global/footer.php');
}
?>

What I am doing is grabbing the address value from the posted form and appending to the geolocurl from the db.  I am trying to load the resulting page back into my page.

An example of how the url would look is: http://maps.googleapis.com/maps/api/geocode/xml?sensor=true&address=london,+england,+united+kingdom

I have tried everything I can think of from iframes to file_get_contents to div to simplexml_load_file but no luck so far.  Any advice you can give me would be greatly appreciated.

Kind Regards,

John
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi Marcus,

Thanks for that.  I had not considered htmlspecialchars.  Just in case anybody else wants to do this, here is the completed code:

if(isset($_POST['generate'])) {
    $address = $_POST['address'];
    $url = $geolocurl.$address;
    $xml = file_get_contents($url);
    echo '<hr class="bottom" /><h3>Geocode Results</h3>'.$geolocurl.$address.'
    <div style="margin-top: 10px; padding: 0 10px 0 10px; border: 1px solid #999999; overflow: auto; width: 748px; height: 400px;"><pre>'. htmlspecialchars($xml).'</pre></div>';
}

Many thanks again for your help with this.

Kind Regards,

John
You're welcome. Thanks for points and good luck with your project.

Cheers
Marco
Hey, John.  Not to chide you, but what you posted is not the completed code.  These are undefined variables.

$url = $geolocurl.$address;

If you had told us that information, we could have given you a working script.  But it is only by luck that we might guess the correct answer without looking at the data.

I expect you're ultimately looking for something more along these lines.  HTH, ~Ray
<?php // RAY_temp_johnformby.php

// MAKE SURE WE GET GOOD DEBUGGING INFORMATION
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

// MAKE IT EASY TO READ
echo "<pre>";

// ACQUIRE THE DATA IN AN OBJECT
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?sensor=true&address=london,+england,+united+kingdom';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);

// ACCESS SOME COMPONENTS OF THE OBJECT WITH OOP NOTATION
foreach ($obj->result as $r)
{
    echo PHP_EOL;
    echo $r->formatted_address;
    echo ' geocode ';
    echo $r->geometry->location->lat;
    echo ',';
    echo $r->geometry->location->lng;
}

echo PHP_EOL . PHP_EOL;

// ACTIVATE THIS TO SHOW WHAT IS IN THE OBJECT
// var_dump($obj);

Open in new window