PHP将HTML代码从SQL导出到XML

时间:2012-01-24 20:56:16

标签: php sql xml

我需要构建我的SQL表的XML文件。 这是我的代码:

   $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()); 

问题是在Item2中有“HTML”代码,就像这样

<b>text</b><br>more text

如何将其导出到我的XML文件?

3 个答案:

答案 0 :(得分:2)

看一下PHP的htmlspecialchars函数。

或者可能是htmlentities

答案 1 :(得分:0)

XML中有5个预定义的实体引用:

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

所以请使用类似str_replace('&amp; lt','&lt;',$ item);

答案 2 :(得分:0)

此功能可以帮助您

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;

}