Problem with simple PHP

Hello

I am new to PHP and maybe my question can be stupid but I really don’t know what is wrong.

I have a database and I am trying to parse its data to XML

So I am using the following code but it keeps giving me an error during parsing.

<?php
require("codes_database.php");

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

// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', 'root');
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\\'t use db : ' . mysql_error());
}

// Select all the rows in the tables
$query1 = "SELECT * FROM busking_denmark WHERE 1";
$query2 = "SELECT * FROM busking_germany WHERE 1";
$query3 = "SELECT * FROM busking_greece WHERE 1";
$query4 = "SELECT * FROM busking_netherlands WHERE 1";
$query5 = "SELECT * FROM busking_uk WHERE 1";
//$query6 = "SELECT * FROM busking_usa WHERE 1";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);
$result4 = mysql_query($query4);
$result5 = mysql_query($query5);
//$result6 = mysql_query($query6);
if (!$result1) {
  die('Invalid query: ' . mysql_error());
}
if (!$result2) {
  die('Invalid query: ' . mysql_error());
}
if (!$result3) {
  die('Invalid query: ' . mysql_error());
}
if (!$result4) {
  die('Invalid query: ' . mysql_error());
}
if (!$result5) {
  die('Invalid query: ' . mysql_error());
}
// if (!$result6) {
//  die('Invalid query: ' . mysql_error());
// }  


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

// Start XML file, echo parent node
echo '<busking_denmark>';
echo '<busking_dgermany>';
echo '<busking_greece>';
echo '<busking_netherlands>';
echo '<busking_uk>';
// echo '<busking_usa>';

// Iterate through the rows, printing XML nodes for each



while ($row1 = @mysql_fetch_assoc($result1)){

  echo '<busking_denmark ';
  echo 'Place="' . parseToXML($row1['Place']) . '" ';
  echo 'Description="' . parseToXML($row1['Description']) . '" ';
  echo 'Lat="' . $row1['Lat'] . '" ';
  echo 'Lng="' . $row1['Lng'] . '" ';
  echo 'Level="' . $row1['Level'] . '" ';
  echo '/>';
}

while ($row2 = @mysql_fetch_assoc($result2)){

echo '<busking_germany ';
  echo 'Place="' . parseToXML($row2['Place']) . '" ';
  echo 'Description="' . parseToXML($row2['Description']) . '" ';
  echo 'Lat="' . $row2['Lat'] . '" ';
  echo 'Lng="' . $row2['Lng'] . '" ';
  echo 'Level="' . $row2['Level'] . '" ';
  echo '/>';
}

while ($row3 = @mysql_fetch_assoc($result3)){

echo '<busking_greece ';
  echo 'Place="' . parseToXML($row3['Place']) . '" ';
  echo 'Description="' . parseToXML($row3['Description']) . '" ';
  echo 'Lat="' . $row3['Lat'] . '" ';
  echo 'Lng="' . $row3['Lng'] . '" ';
  echo 'Level="' . $row3['Level'] . '" ';
  echo '/>';
}

while ($row4 = @mysql_fetch_assoc($result4)){

echo '<busking_netherlands ';
  echo 'Place="' . parseToXML($row4['Place']) . '" ';
  echo 'Description="' . parseToXML($row4['Description']) . '" ';
  echo 'Lat="' . $row4['Lat'] . '" ';
  echo 'Lng="' . $row4['Lng'] . '" ';
  echo 'Level="' . $row4['Level'] . '" ';
  echo '/>';
}

while ($row5 = @mysql_fetch_assoc($result5)){

echo '<busking_uk ';
  echo 'Place="' . parseToXML($row5['Place']) . '" ';
  echo 'Description="' . parseToXML($row5['Description']) . '" ';
  echo 'Lat="' . $row5['Lat'] . '" ';
  echo 'Lng="' . $row5['Lng'] . '" ';
  echo 'Level="' . $row5['Level'] . '" ';
  echo '/>';
}
/*
while ($row6 = @mysql_fetch_assoc($result6)){

echo '<busking_usa ';
  echo 'Place="' . parseToXML($row6['Place']) . '" ';
  echo 'Description="' . parseToXML($row6['Description']) . '" ';
  echo 'Lat="' . $row6['Lat'] . '" ';
  echo 'Lng="' . $row6['Lng'] . '" ';
  echo 'Level="' . $row6['Level'] . '" ';
  echo '/>';
} 

*/


// End XML file
echo '</busking_denmark>';
echo '</busking_germany>';
echo '</busking_greece>';
echo '</busking_netherlands>';
echo '</busking_uk>';
//echo '</busking_usa>';

?>

The error is giving me is

XML Parsing Error: not well-formed
Line Number 1, Column 4694:

It gives me a mistake when is coming to the break tag point ( <br >/ )

Can anybody help me why is this happening ?

Thanks is advance :lol:

The replacement character would need to be escaped or changed to single quotes:

$xmlStr=str_replace("'",''',$xmlStr);

But since it doesn’t actually do anything, you can actually delete that line.

It gives me a mistake when is coming to the break tag point ( <br >/ )

I don’t understand what you mean. Can you show a sample of what you’re getting for output?

Yes of course !!!

This what is give me as output:

XML Parsing Error: not well-formed
Location: http://localhost/BuskingMap/create_xml.php
Line Number 1, Column 4509:

Then is shows a small arrow scrolling under my data and pointing the first “<” at the <br />

I can not copy it as when I paste it here is not looking correct anymore.

Do you understand know ???

Sorry…that still doesn’t give me any more useful information.

Post the output in [ code ] [ /code ] tags like you did with your PHP code. That would probably retain the formatting better.

<busking_denmark><busking_dgermany><busking_greece><busking_netherlands><busking_uk><busking_denmark Place="Copenhagen" Description="Copenhagen is a very nice city with very big pedestrian streets that are perfect for busking. The best pedestrian shopping area suitable for busking is called 'Stroget'.In Danemark it is actually prohibited busking unless you have a permission  but not punished with a fine, until you disobey direct orders from the police. Best time to busk daytime." Lat="55.6792" Lng="12.5811" Level="good" /><busking_germany Place="Karlsruhe" Description="The city of Karlsruhe is not very known among buskers but is very promising, as its main shop street is very crowded and full of life (called Kaiserstrase). Best time to busk 12.00- 18.00." Lat="49.0095" Lng="8.39917" Level="best" /><busking_germany Place="Heidelberg" Description="Heidelberg is one of the most known German cities and a very touristic place with hunderds of visitors every day. The best place to busk is the shop pedestrian street called 'Hauptstrasse'. To get there just take a bus to Bismarckplatz. There are a lot of buskers on this street and so the competition is high. Be careful cause busking is allowded only on specific locations (not too many). Best time to busk 12.00- 18.00, especially on Saturdays" Lat="49.4105" Lng="8.69813" Level="very good" /><busking_germany Place="Cologne" Description="Cologne is a very big city, full of tourists. A good place to busk is in front of the big church in Dom/Hbf main train station. An alternative is to busk in the nearby shop streets called 'Hope strasse' and 'Chlildergasse strasse'. The competition is very high as there are a lot of other buskers . Best time to busk 12.00- 18.00" Lat="50.9408" Lng="6.95663" Level="very good" /><busking_germany Place="Stuttgart" Description="Stuttgart is a big industrial city with a very nice city center. A real jewlery for buskers as people really seams to apreciate music and support street performers. The best place for busking is the main shopping street called 'Konigstrasse' and it start right next to the south-weast exit of the main train station. Best time to busk 12.00- 18.00" Lat="48.7771" Lng="9.17708" Level="best" /><busking_germany Place="Frankfurt" Description="Frankfurt is a city from outer space with very high skyline buildings. Its main shopping street is cail 'Zeil strasse' and is full of people and ideal for busking. Best time to busk 12.00- 18.00" Lat="50.1143" Lng="8.68175" Level="best" /><busking_germany Place="Munich" Description="Munich is the typical big rich city model. The main shopping streets where is posible to busk are 'Neuhauserstrasse' and 'Kaufingerstrasse'.One of the best times to busk in Munich is during 'Oktoberfest' festival which is very famous worldwide. If you decide to busk during this event be carefull cause it is illegal (the money you can gane are very good !!!). If you decide to do it try move a lot and avoid the most center places of the city. Generally best time to busk 12.00- 18.00" Lat="48.1386" Lng="11.5685" Level="very good" /><busking_germany Place="Bonn" Description="Bonn is famous city. Unfortunately we don't have more information about this city at the moment. Although a good spot to busk is near the main train station.(No specific location on the map)" Lat="50.7327" Lng="7.09631" Level="good" /><busking_germany Place="Dresden" Description="Good city to busk but too many buskers and street performers here. So the competition is real high. Also the rules for buskers are quite strict (no more than 3 people bands). Best pitch to busk --> unknown.Best time to busk 12.00- 18.00.(No specific place on the map)" Lat="51.051" Lng="13.7336" Level="good" /><busking_germany Place="Trier" Description="Very beautiful city and a busker\\'s paradise.Best place to busk is behind the 'Kornmarkt' , on 'Fleichstrasse', just in front of the Kaufhof Gallerie.Best time to busk 12.00- 18.00" Lat="49.7558" Lng="6.63916" Level="very good" /><busking_germany Place="Berlin" Description="Berlin is a magical big city very good for busking and street performing in general. Is believed that the best places to busk are the metro station like Alexanderplatz and Postdamer Platz stations. A lot of people also busk within the metro trains even if it's not clear if it is strictly legal. Generally friendly atmosphere and no problems with police and busking permissions.Best time to busk daytime." Lat="52.5218" Lng="13.4106" Level="best" /><busking_greece <br />
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

This is what i get and the problem is at the last command <br /> when it comes to the “<” symbol !

The message i got is

XML Parsing Error: not well-formed
Location: http://localhost/BuskingMap/create_xml.php
Line Number 1, Column 4509:

It looks like this is where the stopping point is:

while ($row3 = @mysql_fetch_assoc($result3)){

echo '<busking_greece ';
  echo 'Place="' . parseToXML($row3['Place']) . '" ';
  echo 'Description="' . parseToXML($row3['Description']) . '" ';
  echo 'Lat="' . $row3['Lat'] . '" ';
  echo 'Lng="' . $row3['Lng'] . '" ';
  echo 'Level="' . $row3['Level'] . '" ';
  echo '/>';
}

But I don’t see anything in your code that would seem to throw in a <br /> tag.

While it’s unrelated to <br /> issue, I 'm not sure what you’re trying to accomplish here:

// Start XML file, echo parent node
echo '<busking_denmark>';
echo '<busking_dgermany>';
echo '<busking_greece>';
echo '<busking_netherlands>';
echo '<busking_uk>';
// echo '<busking_usa>';
// End XML file
echo '</busking_denmark>';
echo '</busking_germany>';
echo '</busking_greece>';
echo '</busking_netherlands>';
echo '</busking_uk>';
//echo '</busking_usa>';

If you’re just trying to encapsulate all of the XML data, you’d just need one parent tag.

You would also need the XML definition tag to make it a proper XML document:

<?xml version="1.0" encoding="UTF-8" ?>

Poking my nose in here a bit FF.

the <br /> is probably the start of the error notice.

  echo 'Place="' . parseToXML($row3['Place']) . '" '; 

this line is probably the error causer, in that case. Check that $row3[‘Place’] exists, and that it’s not slipping any strange XML-breaking characters through the parseToXML function.

I was thinking that too, but wouldn’t the beginning part of the string show up in the echo?

If PHP borked somehow in running parseToXML, it would error before echoing (inside-out stack processing)… tbh, the error he’s seeing doesnt sound like a PHP error, which indicates that it SHOULD have output the whole thing, flawed or not… (For the OP: PHP errors are generally single-line, and less verbose.)

I must say I dont understand the code as generated; It would generate an XML structure like…


<busking_denmark>
  <busking_dgermany>
    <busking_greece>
      <busking_netherlands>
        <busking_uk>
         <busking_denmark (Stuff Here) />

which… surely isnt what you want.

PS: Your “end XML” section is backwards. You need to close UK first, then netherlands, etc etc. (This may be why the page is coming out as improperly formatted - closing the tags in the wrong order)
PPS: Your ‘Begin XML’ section misspells the germany tag.

Hello

I entered the code you provided me

<?xml version="1.0" encoding="UTF-8" ?>

and it gives me a mistake !!!:frowning:

I also closed the XML in the right order.

What else do you think I need to change so my code can work ???

Thanks again

The thing that I am trying to do here is to transform some data from a database that I have created to XML format and I am calling the attribute fields.

Any help please ???

As you no doubt discovered, getting the xml syntax just right can be a pain. Especially for more complicated files.

Take some time and learn to use the dom routines. It may seem a bit overwhelming at first but there is plenty of examples in the manual and plenty of tutorials. Once you have your dom built then writing to xml is trivial.
PHP: DOMDocument::__construct - Manual
http://www.techrepublic.com/article/dynamic-xml-document-construction-with-the-php-dom/6141415


$dom = new DOMDocument('1.0', 'iso-8859-1');

$element = $dom->createElement('test', 'This is the root element!');

// We insert the new element as root (child of the document)
$dom->appendChild($element);

// See the tutorial for an example of adding attributes

echo $dom->saveXML();