Page 1 of 1

Easy XML xmlStr question

Posted: Tue Feb 03, 2009 1:09 pm
by okelly
Dumb question.. I know. Be easy on me...
My php5 code below works fine and generates correct output like this.. http://www.xbrlplanet.org/planet/xbrlprojects2xml.php
Except when I insert the ":" into the "g:markers" echo command below.
Can somebody volunteer the correct syntax so I get <g:markers> <g:markers/> below?

Thanks very much

Code: Select all

 
 <?php include ('../admin/config.php');  
 function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','<',$htmlStr); 
$xmlStr=str_replace('>','>',$xmlStr); 
$xmlStr=str_replace('"','"',$xmlStr); 
$xmlStr=str_replace("'",''',$xmlStr); 
$xmlStr=str_replace("&",'&',$xmlStr); 
return $xmlStr; 
} 
 
// Opens a connection to a MySQL server
$connection=mysql_connect ("$dbhost", "$dbuser", "$dbpass");
if (!$connection) {
  die('Not connected : ' . mysql_error());
}
 
// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
 if (!$db_selected) {
 die ('Can\'t use db database : ' . mysql_error());
 }
 
// Select all the rows in the markers table
$query = "SELECT * FROM Projects WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
 
header("Content-type: text/xml");
 
// Start XML file, echo parent node
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<!--prepared for demo purposes only. -->';
echo '<!--distributed at http://www.xbrlplanet.org -->';
echo '<g:markers>';
 
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'country="' . $row['country'] . '" ';
  echo 'url="' . $row['url'] . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'cat="' . $row['cat'] . '" ';
  echo '/>';
}
 
// End XML file
echo '</g:markers>';
 
?>
 
 

Re: Easy XML xmlStr question

Posted: Thu Feb 05, 2009 9:17 am
by susrisha
hi there, you could have used the simplexml or Dom functions provided default by php. That will be a lot better than writing your own version of xml and its parsing.