Solved

Remove White Space

Posted on 2010-08-16
11
448 Views
Last Modified: 2013-12-13
Looks like your PHP is putting out a space before the <?xml ...?> declaration, which is technically invalid XML

How do I remove that? This is for Google Maps
<?
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius));
$result = mysql_query($query);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content-type: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>

Open in new window


0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 125 total points
ID: 33451214
$dom->saveXML() should never have leading or trailing spaces.
What you need to make sure of, is that there is NO space before the first <? tag (line one of your code) and no space, linebreak or anything after the last ?> of your code.
Because PHP will treat any source code not encased in <? ?> as normal html data and output it to the browser/stream.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33451290
There are no spaces. Do you have any other thoughts?
0
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 125 total points
ID: 33451334
It looks like you may have something after the ?>. You have a line 35 that appears blank, so there was most likely something there in your copy / paste.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 7

Author Comment

by:rgranlund
ID: 33451406
Please have a look again, cause I'm still getting this error:
[code]
<?php
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius));
$result = mysql_query($query);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>
[/code]

It appears to be commented out.
<?php
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", mysql_real_escape_string($center_lat), mysql_real_escape_string($center_lng), mysql_real_escape_string($center_lat), mysql_real_escape_string($radius));
$result = mysql_query($query);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 33451414
Also, it works fine in IE but not in Firefox.
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33467206
can you post the xml output from firebug?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33467286
white space problem solved
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33467336
what was it?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33468659
There was a space after the ?> in the include file.  It only took me about 8 hours to find it.  Geez.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34881964
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question