0

我必须从 Mysql 数据库开始创建一个 Xml 文件。我正在使用 php 脚本来生成 xml 文件。这是我用来生成我的 xml 文件的 php 代码部分:

// create a new XML document
$doc = new DomDocument('1.0', 'UTF-8');

// create root node
$root = $doc->appendChild($doc->createElement('rss'));
$root->setAttribute('version', '2.0');
$root->setAttribute('encoding', 'UTF-8');

// create children
while($row = mysql_fetch_assoc($result)) {
   // add node for each row
  $occ = $doc->createElement('item');
  $occ = $channelNode->appendChild($occ);


  // add a child node for each field

  foreach ($row as $fieldname => $fieldvalue) {


        if($fieldname =='description')   {

          $newcdata=$doc->createCDATASection($fieldvalue);
          $child = $doc->createElement($fieldname);
          $child = $occ->appendChild($child);
          $value = $doc->createTextNode($fieldvalue);
          $value = $child->appendChild($newcdata);
        }
        else {
          $child = $doc->createElement($fieldname);
          $child = $occ->appendChild($child);
          $value = $doc->createTextNode($fieldvalue);
          $value = $child->appendChild($value);
        }



      } // foreach

} // while

这个程序运行良好。问题发生在数据库中有特殊字符要插入 XML 文件的情况下,在这种情况下,我尝试使用插入数据CDATA但它不起作用。那么,如何将特殊字符插入 Xml 文件?

4

1 回答 1

0

It is not possible to insert certain characters into an XML document. The work-around is for both the sender and receiver to agree on how they will be encoded. Common options include Base64 and Ascii85.

It might also be possible to do simple character substitution such as using the Unicode control pictures. For example, ␀ \u2400 for \0 and ␇ \u2407 for \b. That keeps it human readable but still would require the sender and receiver to agree.

于 2013-04-07T15:41:58.687 回答