Php XML challenge

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
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Php XML challenge

Post by batowiise »

Hi All,

The following code outputs in XML format and its working fine;

Code: Select all



require("xml_dbcredential.php"); 

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server

$connection=mysql_connect (localhost, $username, $password);
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 markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

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

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

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("name",$row['name']); 
  $newnode->setAttribute("type", $row['type']);
} 

echo $dom->saveXML();



The output XML format:

-<markers>
<marker name="Apaapa" type="CA"/>
<marker name="Maami" type="CC"/>
<markers>

I however want to use two tables and get the following XML output but
cannot find my way out:

-<markers>
<marker name="Apaapa" type="CA"/>
<marker name="Maami" type="CC"/>
-<line colour="#000" width="2">
<point lat="5.876435" lng="-0.847364"/>
<point lat="5.855505" lng="-0.456736"/>
</line>
-<line colour="#000" width="2">
<point lat="5.800000" lng="-0.877304"/>
<point lat="5.832321" lng="-0.874600"/>
</line>
<markers>

Your help is mush appreciated.

Regards.
Isaac
Post Reply