XML content possible to have a string data?



Can an xml content possible to contain string data



<marker name="San Sebastian Church" address="Pasaje del Carmen Street, Quiapo, Manila, Metro Manila" lat="14.599667" lng="120.989044" info="This is a church found in Quiapo" tts="200" distance="1.9062626408367087" type="church"/>


on my marker element I have problems whenever I try to use setAttribute on info and type. When I try to put them and run the php script I always get


XML Parsing Error: junk after document element Location: some location with the get method Line Number 2, Column 1:Warning: DOMElement::setAttribute(): string is not in UTF-8 in *some location of the php file on line 42

^


here is my php code:



<?php
require('phpDatabaseCredentials.php');
$link = mysqli_connect($host,$username,$password,$database);
if(!$link){
die ('unable to connect to the database' . mysqli_connect_error());
}

//Get parameters from URL
$myLat = $_GET['lat'];
$myLng = $_GET['lng'];
$calcDistance = $_GET['radius'];

//Start XML file, create parent XML node
$dom = new DOMDocument('1.0','UTF-8');
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

//Search the rows in the markers table
$query = sprintf("SELECT siteName,address,latitude,longitude,info,tts, (6371 * acos(cos(radians('%s')) * cos(radians(latitude)) * cos(radians(longitude) - radians ('%s')) + sin(radians('%s')) * sin(radians(latitude))))AS distance FROM maptable HAVING distance < '%s' ORDER BY distance LIMIT 0, 50",
mysqli_real_escape_string($link, $myLat),
mysqli_real_escape_string($link, $myLng),
mysqli_real_escape_string($link,$myLat),
mysqli_real_escape_string($link, $calcDistance));

$result = mysqli_query($link, $query);

$row_cnt = mysqli_num_rows($result);
if(!$result){
die("Invalid query: " . mysqli_error());
}

header("content-type: text/xml; charset: utf-8");

//iterate through the rows, adding XML nodes for each
while($row = mysqli_fetch_assoc($result)){
$node = $dom -> createElement("marker");
$newnode = $parnode -> appendChild($node);
$newnode->setAttribute("name",$row['siteName']);
$newnode->setAttribute("address",$row['address']);
$newnode->setAttribute("lat",$row['latitude']);
$newnode->setAttribute("lng",$row['longitude']);
$newnode->setAttribute("info",$row['info']);
$newnode->setAttribute("tts",$row['tts']);
$newnode->setAttribute("type",$row['markerType']);
$newnode->setAttribute("distance",$row['distance']);
}

echo $dom->saveXML();

?>


and here is a sample content of my database:



id = 31
siteName = Rizal Park
latitude = 14.582747
longitude =120.978310
address = "Roxas Boulevard, Ermita, Manila, Metro Manila"
info = "One of the famous and historical park in the Philippines.
The place where Dr. Jose Rizal was executed."
tts = null
type = "Historical Monument / Park"


I've read some on the internet saying that xml is strict when it comes to special characters. When I try commenting out the info and type the xml works. Besides re-writing the content is there any way that I can use to make the xml accept my strings? I don't want to use json because I am not that good at javascripting. Thanks


No comments:

Post a Comment