Search dynamic XML generated by PHP

A bit of background: I’m creating a google map that places markers via PHP/Javascript. These markers are brought in from a MYSQL table and placed using their latitude and longitude. I followed a Google tutorial on how to do this and it’s working fine. The way it does this is a PHP file (called genxml.php) creates a XML for JS to read and the dynamically place on the map. All good. Now here the problem comes in.

Obviously, a whole heaps of markers on a map isn’t a huge lot of good, well because they are just markers. Yes ok, they have labels (when you click on them) but trying to find a particular marker is not easy - there are around 2000 of them.

So after studying the JS code a bit, I noticed that the center of the map is set via a lng & lat in the JS. So I added some PHP into it with GET, and so the url became something like this map.php?lng=55.2&lat=-1.2

What I want to do (and am stuck on) is create a search box, that suggests names (from the relevant column in MYSQL) and when you click on them, the a tag is set something like this: <a href="?lng=$lng&lat=$lat"> with the $lng and the $lat obviously filled out by the numbers.

I followed this tutorial (http://www.w3schools.com/php/php_ajax_livesearch.asp) which searches an XML file, but when I reference with PHP file (which if you open it in a browser shows a XML file) it doesn’t work.

So if anyone could help me with this it would be great :smile:

A few points to help:

The PHP file is brought in like this:

downloadUrl("genxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");

genxml.php says this, but works fine (I believe it’s just I haven’t escaped & and / etc)

This page contains the following errors:
error on line 1 at column 3427: Encoding error
Below is a rendering of the page up to the first error.`

The format of the XML file is the following:

<markers>
<marker name="" address="" lat="" lng="" type=""/>
</markers>`

I am indefinably not a PHP power user so lil bits of code with any explanations would be great :wink:
Any other info needed pls lmk
Thanks in advance :slight_smile:

When you go directly to genxml.php in the browser do you see the below error or something similar.

This page contains the following errors:
error on line 1 at column 3427: Encoding error
Below is a rendering of the page up to the first error.

Yes, exactly that - in a big red box. And only that - nothing below (like it would indicate)

Is it that that is mucking it up? Because I don’t normally have to go to genxml.php - the map.php includes it automatically - and map.php works fine

Well you’re missing the doctype for the XML document.

<?xml version="1.0"?>
<markers>
<marker name="" address="" lat="" lng="" type=""/>
</markers>

You should also add a header for the content type in the PHP script responsible for generating the XML. Make sure you place that code BEFORE any echo, print or any other functions that send output otherwise an error will occur.

header('Content-Type: text/xml');

No I’m not - this is the generator part of the php file

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo '</markers>';

And I haven’t just added that - that’s how it was

If you are trying to fix the error that genxml gives - I’m not sure why, but replacing

parseToXML($row['name'])

with

parseToXML('&','&amp;', $row['name'])

makes it work, but all the names show up as &

The parseToXML function is as follows:

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&apos;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
}

That solution is brittle as it doesn’t begin to cover the entire gamut of possibilities. Either wrap every line in CDATA or use DomDocument.

1 Like

I fully agree with that and it is not a solution. As I mentioned in the initial post, I was following a Google Tutorial to create the Map Markers bit - which this is part of. The last step of this is to replace

parseToXML('&','&amp;', $row['name'])

with

parseToXML($row['name'])

which gives it the error mentioned. By reversing this, the error goes but I lose the names. Anyhow, with both of these the map marker still works.

As to the original query, I have decided to simply write a new query (new php file) to handle the search, and this goes straight to MySQL cutting out the XML completely.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.