Getting error in php (xml:parser well-not formed)

Hi All, I am getting the error in php (postgres connection string) as mentioned below :

XML Parsing Error: not well-formed
Location: http://localhost:xyz.php
Line Number 1, Column 5: … i.e.

Here is my code :
1 <?php
2 // Database connection settings
3 define(“PG_DB” , “testgis”);
4 define(“PG_HOST”, “localhost”);
5 define(“PG_USER”, “postgres”);
6 define(“PG_PORT”, “5432”);
7 define(“TABLE”, “roads”);
8 define(“PWD”, “root”);
9 $counter = $pathlength = 0;
10
11 // Retrieve start point
// $start = split(’ ',$_REQUEST[‘startpoint’]);
$startPoint = array(-6.466140625, 1.13427734375);

	// Retrieve end point
	//  $end = split(' ',$_REQUEST['finalpoint']);
	$endPoint = array(21.5389765625, 18.993828125);
	// Find the nearest edge
	$startEdge = findNearestEdge($startPoint);
	$endEdge   = findNearestEdge($endPoint);
	

	// FUNCTION findNearestEdge
	function findNearestEdge($lonlat) {
    // Connect to database
    $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER." password=".PWD);
    $sql = "SELECT gid, source, target, the_geom,
                     distance(the_geom, GeometryFromText(
                  'POINT(".$lonlat[0]." ".$lonlat[1].")', -1)) AS dist
            FROM ".TABLE."
            WHERE the_geom && setsrid(
                  'BOX3D(".($lonlat[0]-200)."
                         ".($lonlat[1]-200).",
                         ".($lonlat[0]+200)."
                         ".($lonlat[1]+200).")'::box3d, -1)
            ORDER BY dist LIMIT 1";
    $query = pg_query($dbcon,$sql);
    $edge['gid']      = pg_fetch_result($query, 0, 0);
    $edge['source']   = pg_fetch_result($query, 0, 1);
    $edge['target']   = pg_fetch_result($query, 0, 2);
    $edge['the_geom'] = pg_fetch_result($query, 0, 3);
	
	echo "edge=" . $edge['gid'] . "&lt;/br&gt;";
	echo "source=" . $edge['source'] . "&lt;/br&gt;";
	echo "target=" . $edge['target'] . "&lt;/br&gt;";
	echo "the_geom=" . $edge['the_geom'] . "&lt;/br&gt;";
	
    // Close database connection
    pg_close($dbcon);
    return $edge;
}	
		echo "startedge=" . $startEdge['source'] . "&lt;/br&gt;";
		echo "endedge=" . $endEdge['target'] . "&lt;/br&gt;";

		// Select the routing algorithm
		$sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
               length(rt.the_geom) AS length, ".TABLE.".id
            FROM ".TABLE.",
                (SELECT gid, the_geom
                    FROM dijkstra_sp_delta(
                        '".TABLE."',
                        ".$startEdge['source'].",
                        ".$endEdge['target'].",
                        3000)
                 ) as rt
            WHERE ".TABLE.".gid=rt.gid;";
			
  	// close switch	
	// Database connection and query
	$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER." password=".PWD);

	$query = pg_query($dbcon,$sql);
	
	// Return route as XML
$xml = '&lt;?xml version="1.0" entity_encoding="UTF-8" standalone="yes" ?&gt;'."\

";
$xml .= "<route>
";

	// Add edges to XML file
	while($edge=pg_fetch_assoc($query)) {

	$pathlength += $edge['length'];

	$xml .= "\	&lt;edge id='".++$counter."'&gt;\

“;
$xml .= “\ \ <id>”.$edge[‘id’].”</id>
“;
$xml .= “\ \ <wkt>”.$edge[‘wkt’].”</wkt>
“;
$xml .= “\ \ <length>”.round(($pathlength/1000),3).”</length>
";
$xml .= "\ </edge>
";
}

	$xml .= "&lt;/route&gt;\

";

	// Close database connection
	pg_close($dbcon);

	// Return routing result
	header('Content-type: text/xml',true);
	echo $xml;

?&gt;

Here is my output:
edge=43</br>source=37</br>target=32</br>the_geom=0105000000010000000102000000060000004C60BAF79BFE26C0AB758262C883B0BF94154C4DC8F324C032899FD65E16CD3FC145A98EFDEF21C0471698AEA01ADE3FD0E9F797CF741DC0253C44196163E83F4A50F132FC9718C094369EEDB8DCF03FD4390ADA895014C0574EDC4003280240</br>edge=28</br>source=22</br>target=25</br>the_geom=0105000000010000000102000000030000000000000000002F400000000000002F400000000000003340000000000000334000000000008037400000000000003340</br>startedge=37</br>endedge=25</br><?xml version=“1.0” entity_encoding=“UTF-8” standalone=“yes” ?>
<route>
<edge id=‘1’>
<id>43</id>
<wkt>MULTILINESTRING((-11.497283689023 -0.064510845232198,-10.4761375575425 0.227245192333677,-8.9687313634521 0.470375223638574,-7.36407315683978 0.762131261204449,-6.1484230003153 1.05388729877032,-5.07865086257376 2.26953745529481))</wkt>
<length>0.007</length>
</edge>
<edge id=‘2’>
<id>38</id>
<wkt>MULTILINESTRING((-4 4,-4.46274864568763 3.93807848556878,-4.75377039792406 3.78754999303269,-4.90429889046015 3.59688056915365,-4.9946159859818 3.4563873094533,-5.10500354717493 3.15533032438113,-5.14514447851788 2.8643085721447,-5.15517971135362 2.53314588856532,-5.07865086257376 2.26953745529481))</wkt>
<length>0.01</length>
</edge>

Pl help me to resolve this is problem.

Thanks in advance.

What happens if you only return only the XML instead of all those other echo lines previously?

If you still get a problem with the xml, run it through an online xml validator and see if that spots the error.

I am using the online validation software :
But again getting the same error, I did not understand why this error comes in xml validation…
Every line has some error, it cann’t read $xml ?? Is there any other format in php? Pl help …

Seeking your early response.

$xml = <?xml version=“1.0” entity_encoding=“UTF-8” standalone=“yes” ?>
$xml .= "<route>
";

	while($edge=pg_fetch_assoc($query)){
	$pathlength += $edge['length'];

	$xml .= "\	&lt;edge id='".++$counter."'&gt;\

“;
$xml .= “\ \ <id>”.$edge[‘id’].”</id>
“;
$xml .= “\ \ <wkt>”.$edge[‘wkt’].”</wkt>
“;
$xml .= “\ \ <length>”.round(($pathlength/1000),3).”</length>
";
$xml .= "\ </edge>
";
}

	$xml .= "&lt;/route&gt;\

";

	// Close database connection
	pg_close($dbcon);

	// Return routing result
	header('Content-type: text/xml',true);
	echo $xml;

Did you try just using double quotes instead of singles on the attributes?

“\ <edge id=\”“.++$counter.”\">
";

You also need to change ‘entity_encoding’ to “encoding” as shown below

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<route>
	<edge id='1'>
		<id>43</id>
		<wkt>MULTILINESTRING((-11.497283689023 -0.064510845232198,-10.4761375575425 0.227245192333677,-8.9687313634521 0.470375223638574,-7.36407315683978 0.762131261204449,-6.1484230003153 1.05388729877032,-5.07865086257376 2.26953745529481))</wkt>
		<length>0.007</length>
	</edge>
	<edge id='2'>
		<id>38</id>
		<wkt>MULTILINESTRING((-4 4,-4.46274864568763 3.93807848556878,-4.75377039792406 3.78754999303269,-4.90429889046015 3.59688056915365,-4.9946159859818 3.4563873094533,-5.10500354717493 3.15533032438113,-5.14514447851788 2.8643085721447,-5.15517971135362 2.53314588856532,-5.07865086257376 2.26953745529481))</wkt>
		<length>0.01</length>
	</edge>
</route>

Hi all,
I Think the error comes in this section as mention below:

It cann’t read while loop.

while($edge=pg_fetch_assoc($query)) {
$pathlength += $edge[‘length’];
$xml .= “\ <edge id='”.++$counter."'>
“;
$xml .= “\ \ <id>”.$edge[‘id’].”</id>
“;
$xml .= “\ \ <wkt>”.$edge[‘wkt’].”</wkt>
“;
$xml .= “\ \ <length>”.round(($pathlength/1000),3).”</length>
";
$xml .= "\ </edge>
";
}
$xml .= "</route>
";
pg_close($dbcon);
header(‘Content-type: text/xml’,true);
echo $xml;

Thanks for replying my questions.

You won’t get an XML parsing error in your while loop unless your query is returning an XML stream. The error you gave, indicates a XML parsing issue in the browser, not within PHP.

How can I resolve this issue ???

Pl suggest…

Change this line

$xml = <?xml version="1.0" entity_encoding="UTF-8" standalone="yes" ?>

To:

$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

Done…

But again getting the same error…

I am getting the output :
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?><route>

The output is in-completed…

Pl resolve this issue.

That is all you are getting for the output? It should have written the closing </route> tag at least too.

Chances are if it is not running the while loop, your query is either returning zero results, or you have a database error that isn’t being captured.

Here is my final output:

<?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?>
<route><edge id=‘1’><id>43</id>
<wkt>MULTILINESTRING((-11.497283689023 -0.064510845232198,-10.4761375575425 0.227245192333677,-8.9687313634521 0.470375223638574,-7.36407315683978 0.762131261204449,-6.1484230003153 1.05388729877032,-5.07865086257376 2.26953745529481))</wkt>
<length>0.007</length>
</edge>
</route>

As while loop is not working properly, I’m unable to get the remaining response.
The remaining output comes in error console of the browser. Also database is working fine.
Why while loop is not working properly ??
Do you have any idea ??

It’s worth pointing out from your original output that </br> is not a valid tag. <br /> is. (</br> would imply a closing tag for a <br> opening tag. BR is not a container tag.)

Can you go into more detail as to why you feel the while look is not working properly? Because it is only putting one <edge> node in the XML output and you are expecting more?

If so, that comes back to the results returned by the query. Are you sure you are getting multiple rows back?

To check, change this code

while($edge=pg_fetch_assoc($query)) {

$pathlength += $edge['length'];

$xml .= "\	<edge id='".++$counter."'>\
";
$xml .= "\	\	<id>".$edge['id']."</id>\
";
$xml .= "\	\	<wkt>".$edge['wkt']."</wkt>\
";
$xml .= "\	\	<length>".round(($pathlength/1000),3)."</length>\
";
$xml .= "\	</edge>\
";
}

To

var_dump(pg_fetch_assoc($query)); // Note this will break your XML output (parse error will occur, do a view source to see the output)
while($edge=pg_fetch_assoc($query)) {

$pathlength += $edge['length'];

$xml .= "\	<edge id='".++$counter."'>\
";
$xml .= "\	\	<id>".$edge['id']."</id>\
";
$xml .= "\	\	<wkt>".$edge['wkt']."</wkt>\
";
$xml .= "\	\	<length>".round(($pathlength/1000),3)."</length>\
";
$xml .= "\	</edge>\
";
}

As per your code I’m getting the only one edge value node in the XML output.
But, I need the remaining output too. Its just because of while loop is not working or anything else ???
Pls help to resolve this loop problem.

Thanks for your support.

What is the remaining output you are expecting to receive?

The remaining output will be :
<edge id=‘2’>
<id>32</id>
<wkt>MULTILINESTRING((-4 4,-2.8 6.7,0 7.5))</wkt>
<length>0.008</length>
</edge>
<edge id=‘3’>
<id>3</id>
<wkt>MULTILINESTRING((0 7.5,3 7.5))</wkt>
<length>0.011</length>
</edge>
<edge id=‘4’>
<id>7</id>
<wkt>MULTILINESTRING((5 9,4 9,3 8,3 7.5))</wkt>
<length>0.014</length>
</edge>
<edge id=‘5’>
<id>18</id>
<wkt>MULTILINESTRING((5 9,5 14.5,8 14.5))</wkt>
<length>0.023</length>
</edge>
<edge id=‘6’>
<id>20</id>
<wkt>MULTILINESTRING((8 14.5,12 14.5))</wkt>
<length>0.027</length>
</edge>
<edge id=‘7’>
<id>25</id>
<wkt>MULTILINESTRING((14 16,13 16,12 15,12 14.5))</wkt>
<length>0.03</length>
</edge>
<edge id=‘8’>
<id>24</id>
<wkt>MULTILINESTRING((15.5 15.5,15 16,14 16))</wkt>
<length>0.031</length>
</edge>
<edge id=‘9’>
<id>28</id>
<wkt>MULTILINESTRING((15.5 15.5,19 19,23.5 19))</wkt>
<length>0.041</length>
</edge>
</route>

Pl help…

Are you trying to do math with the $edge[‘length’] string without casting it to a numeric?

If you are only getting 1 edge value in the var_dump command I gave you to output, you need to take a look at your query again. Make sure your query is returning multiple rows and not just a single row.

I am getting the remaining output in my error console, that means query is running from database. I did not get the next value …I am sharing with you screenshot for your reference.