Reputation: 233
I need to build a XML file of my SQL table. This is my code:
$domtree = new DOMDocument('1.0', 'UTF-8');
$xmlRoot = $domtree->createElement("xml");
$xmlRoot = $domtree->appendChild($xmlRoot);
$query = "SELECT * FROM table1 order by Id";
$result = mysql_query($query,$GLOBALS[dbhand]);
$numRows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
$currentTrack = $domtree->createElement("Orders");
$currentTrack = $xmlRoot->appendChild($currentTrack);
$currentTrack->appendChild($domtree->createElement('Item1',$row["Item2"]));
$currentTrack->appendChild($domtree->createElement('Item2',$row["Item2"]));
}
echo utf8_encode($domtree->saveXML());
The problem is that in Item2 there is "HTML" code, like this
<b>text</b><br>more text
What do I have to do to get this exported to my XML file??
Upvotes: 0
Views: 351
Reputation: 1
This function may help you
function philsXMLClean($strin) {
$strout = null;
for ($i = 0; $i < strlen($strin); $i++) {
switch ($strin[$i]) {
case '<':
$strout .= '<';
break;
case '>':
$strout .= '>';
break;
case '&':
$strout .= '&';
break;
case '"':
$strout .= '"';
break;
default:
$strout .= $strin[$i];
}
}
return $strout;
}
Upvotes: 0
Reputation: 1776
There are 5 predefined entity references in XML:
< < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
so use something like str_replace('<', '<', $item);
Upvotes: 0
Reputation: 14479
Take a look at PHP's htmlspecialchars function.
Or possibly htmlentities.
Upvotes: 2