【发布时间】:2011-03-21 11:08:15
【问题描述】:
运行此代码时出现错误.. 当数据库中没有特殊字符时,我运行良好。如果它有特殊字符那么我得到了错误请解决我非常感谢。
当数据库中的任何特殊字符(例如“'&?” CDDATA 也不适用于我的代码,我检查了它。请告诉我一些事情我怎样才能使错误更少的xml..
以下是代码:
$file= fopen("../xml/{$productID}.xml" , "w");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
$_XML = "<!DOCTYPE Server SYSTEM \"opt/pdos/etc/pdoslrd.dtd\">";
$_xml .="<productsnode>";
while ($row = mysql_fetch_array($productQuery, MYSQL_ASSOC)) {
$_xml .=" <product>";
$_xml .=" <productID>" . $row['productID'] . "</productID>";
$_xml .=" <productName>" . htmlspecialchars($row['productName']) . "</productName>";
$_xml .=" <productDescription>" . htmlspecialchars($row['productDescription']) . "</productDescription>";
$_xml .=" <productPicture>" . htmlspecialchars($row['productPic']) . "</productPicture>";
$_xml .=" <category>";
$_xml .=" <categoryID>" . $row['categoryID'] . "</categoryID>";
$_xml .=" <categoryName>" . htmlspecialchars($row['categoryName']) . "</categoryName>";
$_xml .=" <categoryDescription>" . htmlspecialchars($row['categoryDiscription']) . "</categoryDescription>";
$_xml .=" <categoryPicture>" . htmlspecialchars($row['categoryPic']) . "</categoryPicture>";
$_xml .=" <subCategory>";
$_xml .=" <subCategoryID>" . $row['subCategoryID'] . "</subCategoryID>";
$_xml .=" <subCategoryName>" . htmlspecialchars($row['subCategoryName']) . "</subCategoryName>";
$_xml .=" <subCategoryDetail>" . htmlspecialchars($row['subCategoryDescription']) . "</subCategoryDetail>";
$_xml .=" </subCategory>";
$_xml .=" </category>";
$_xml .=" </product>";
}
$_xml .="</productsnode>";
fwrite($file, $_xml);
fclose($file);
【问题讨论】:
-
什么错误?不要无知。 ;)
-
Assigning xml generated by a while loop to a variable 的可能副本。换句话说,使用 DOM 或 XMLWriter。
-
您是否运行了查询
SET NAMES 'utf8'? -
@Gordon,你是看问题还是只看标题?
-
@jswolf 两者。我浏览了示例代码并注意到它使用字符串连接并且理解 OP 在特殊字符方面存在问题,这带来了一个问题,为什么他/她没有为这些事情使用更合适的工具,例如DOM 或 XMLWriter。链接的 dup 显示了如何使用它的示例。此外,在使用 XML (!= HTML) 时使用
htmlspecialchars毫无意义。