I am creating XML using a while loop, but now I need to prepend and append the generated XML with the XML header info and wrapper tag, but I am struggling to get it to work, here is my code,
$result = mysql_query("SELECT * FROM users")
or die(mysql_error());
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$pumaXML = "<userDetails>";
$pumaXML .= "<userID>".$row['uid']."</userID>";
$pumaXML .= "<userName>".$row['userName']."</userName>";
$pumaXML .= "<points>".$row['points']."</points>";
$pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
$pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
$pumaXML .= "</userDetails>";
};
I can’t seem to find a way to do this, I also tried making a function, but that didn’t go that well, here is that code,
function createXML($result) {
while ($row = mysql_fetch_array($result)) {
$pumaXML = "<userDetails>";
$pumaXML .= "<userID>".$row['uid']."</userID>";
$pumaXML .= "<userName>".$row['userName']."</userName>";
$pumaXML .= "<points>".$row['points']."</points>";
$pumaXML .= "<imageURL>".$row['imageURL']."</imageURL>";
$pumaXML .= "<thumbURL>".$row['thumbURL']."</thumbURL>";
$pumaXML .= "</userDetails>";
};
return $pumaXML;
};
Thanx in advance!
Here is how to do it with DOM:
Note that the function expects you to pass in the full result array, so I could test it with:
The function will then return
Please notice that DOM escaped the special chars in John Doe’s name for you automatically. DOM will also make sure the XML element names (or attributes if you use them) are syntactically valid. It also added the XML Prolog.