Easy XML xmlStr question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
okelly
Forum Commoner
Posts: 29
Joined: Thu Feb 23, 2006 2:18 pm

Easy XML xmlStr question

Post 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>';
 
?>
 
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Easy XML xmlStr question

Post 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.
Post Reply