通过 SQL 从 PHP 生成 XML [英] Generate XML from PHP via SQL

查看:30
本文介绍了通过 SQL 从 PHP 生成 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行此代码时出错..当数据库中没有特殊字符时我运行良好.如果它有特殊字符那么我得到了错误请解决我非常感谢.

i have a error when running this code .. i run fine when there is no special characteres in database. if its has special characters then i got the error please solve me i am very thankful.

当数据库中的任何特殊字符(如'&?")出现以下错误时,我不知道为什么会出现这些错误......而且我没有使用 DOM 或 XMLWrite 只是简单地通过文件创建 xml,并澄清 1 件事CDDATA 也不适用于我的代码,我检查了它.请告诉我一些事情,我怎样才能使错误更少的 xml..

Following Error are occured when any special charachers in database like " ' & ? " i don't why those error come .. and i am not using DOM or XMLWrite just simple create the xml via file, and clerify 1 thing that CDDATA also not working for my code i check it. Please tell me some thing how could i make the xml with error less..

代码如下:

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

推荐答案

特殊字符在 xml 中是非法的,因此您需要将所有内容包装在 CDATA 标记之间

Special chars are illegal in xml so you need to wrap everything between CDATA tags

例如:

$_xml .="   <subCategoryName><![CDATA[" . htmlspecialchars($row['subCategoryName']) . "]]></subCategoryName>"; 

阅读更多 -> http://www.w3schools.com/xml/xml_cdata.asp

你的代码应该是这样的:

your code should be something like:

$_xml .=" <product>";
        $_xml .="   <productID><![CDATA[" . $row['productID'] . "]]></productID>";
        $_xml .="   <productName><![CDATA[" . htmlspecialchars($row['productName']) . "]]></productName>";          
        $_xml .="   <productDescription><![CDATA[" . htmlspecialchars($row['productDescription']) . "]]></productDescription>"; 
        $_xml .="   <productPicture><![CDATA[" . htmlspecialchars($row['productPic']) . "]]></productPicture>";
        $_xml .=" <category>";
        $_xml .="   <categoryID><![CDATA[" . $row['categoryID'] . "]]></categoryID>";
        $_xml .="   <categoryName><![CDATA[" . htmlspecialchars($row['categoryName']) . "]]></categoryName>";   
        $_xml .="   <categoryDescription><![CDATA[" . htmlspecialchars($row['categoryDiscription']) . "]]></categoryDescription>";  
        $_xml .="   <categoryPicture><![CDATA[" . htmlspecialchars($row['categoryPic']) . "]]></categoryPicture>";              
        $_xml .=" <subCategory>";
        $_xml .="   <subCategoryID><![CDATA[" . $row['subCategoryID'] . "]]></subCategoryID>";
        $_xml .="   <subCategoryName><![CDATA[" . htmlspecialchars($row['subCategoryName']) . "]]></subCategoryName>";  
        $_xml .="   <subCategoryDetail><![CDATA[" . htmlspecialchars($row['subCategoryDescription']) . "]]></subCategoryDetail>";               
        $_xml .=" </subCategory>";
        $_xml .=" </category>";
        $_xml .=" </product>";

如果这不起作用,则意味着还有其他事情,我建议您复制您得到的错误并将其放入您的答案中,并可能对数据库中存储的内容进行截图.如果您需要帮助,请向我们提供更多详细信息

If that doesn't work it means there is something else, I suggest you copy the error you get and put it in your answer and maybe take a screenshot of what is stored in the database. If you want help give us more details

这篇关于通过 SQL 从 PHP 生成 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆