Mike
Mike

Reputation: 6839

Error when creating xml from mySQL database with php

I am following this tutorial from google for google maps:

https://developers.google.com/maps/articles/phpsqlajax_v3?hl=es

I formatted my code as so:

echo '<markers>';

// Iterate through the rows, adding XML nodes for each
for($i = 0; $i< $breweryNum; $i++ ){

    echo '<marker ';
    echo 'name="' . parseToXML($row['beerBrewery']) . '" ';
    echo 'lat="' . $row['lat'] . '" ';
    echo 'lng="' . $row['longi'] . '" ';
    echo '/>';


}

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

But when I run the php file to test my xml, I get this:

XML Parsing Error: not well-formed
Location: http://beerportfolio.com/getMapMarkers.php
Line Number 1, Column 18:<markers><marker <br />
-----------------^

Upvotes: 0

Views: 321

Answers (2)

First up, XML files often begin with their type declaration. This isn't required but is helpful (i.e. <?xml version="1.0" encoding="UTF-8" ?> )

You seem to have <br /> being output within your xml. You may find it simpler to instead do something like

$strOutput = '<markers>';

And then

$strOutput .= '<marker name="' . parseToXML($row['beerBrewery']) . '" lat="' . $row['lat'] . '" lng="' . $row['longi'] . '" />';

And closing off with

$strOutput = '</markers>';
echo $strOutput;

Upvotes: 0

Are you use nl2br or some to output resulting string?

It's clear error message, after "<markers><marker" outputs tag "<br />" but here should be your "name" attribute. Anyway, you can use following code to not output wrong things:

$out = '<markers>';
for($i = 0; $i< $breweryNum; $i++ ){

    $out .= '<marker ';
    $out .= 'name="' . parseToXML($row['beerBrewery']) . '" ';
    $out .= 'lat="' . $row['lat'] . '" ';
    $out .= 'lng="' . $row['longi'] . '" ';
    $out .= '/>';

}
$out .= '</markers>';

Upvotes: 2

Related Questions