Svein Jarle
Svein Jarle

Reputation: 233

PHP export HTML code from SQL to XML

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

Answers (3)

KHALID
KHALID

Reputation: 1

This function may help you

function philsXMLClean($strin) {
    $strout = null;

    for ($i = 0; $i < strlen($strin); $i++) {
                    switch ($strin[$i]) {
                            case '<':
                                    $strout .= '&lt;';
                                    break;
                            case '>':
                                    $strout .= '&gt;';
                                    break;
                            case '&':
                                    $strout .= '&amp;';
                                    break;
                            case '"':
                                    $strout .= '&quot;';
                                    break;
                            default:
                                    $strout .= $strin[$i];
                    }
    }

    return $strout;

}

Upvotes: 0

Ties
Ties

Reputation: 1776

There are 5 predefined entity references in XML:

&lt;      <    less than
&gt;      >    greater than
&amp;     &    ampersand 
&apos;    '    apostrophe
&quot;    "    quotation mark

so use something like str_replace('&lt', '<', $item);

Upvotes: 0

Travesty3
Travesty3

Reputation: 14479

Take a look at PHP's htmlspecialchars function.

Or possibly htmlentities.

Upvotes: 2

Related Questions