Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php和mysql创建xml文件?_Php_Mysql_Xml_Database - Fatal编程技术网

如何使用php和mysql创建xml文件?

如何使用php和mysql创建xml文件?,php,mysql,xml,database,Php,Mysql,Xml,Database,我试图使用php从本地数据库中存储的数据生成一个xml文件。但我有个问题。当我硬编码该值时,我能够获取xml文件,但当我试图从数据库调用时,我却无法这样做?有人能帮我吗 这是我写的代码 <?php include 'config.php'; include 'database.php'; $sql = "select title, content, url from table limit 100"; $results = Database::GetAll($sql); $xml

我试图使用php从本地数据库中存储的数据生成一个xml文件。但我有个问题。当我硬编码该值时,我能够获取xml文件,但当我试图从数据库调用时,我却无法这样做?有人能帮我吗

这是我写的代码

<?php

 include 'config.php';
 include 'database.php';

$sql = "select title, content, url from table limit 100";
$results = Database::GetAll($sql);

$xml = new DomDocument("1.0","UTF-8");

$content = $xml->createElement("content");
$content = $xml->appendChild($content);

foreach($results as $result) {
    $item = $xml->createElement("item");
    $item = $content->appendChild($item);

    $title = $xml->createElement("title",$result['title']);
    $title = $item->appendChild($title);

    $description = $xml->createElement("description",$result['content']);
    $description = $item->appendChild($description);

    $link = $xml->createElement("link",htmlspecialchars($result['url']));
    $link = $item->appendChild($link);
}

$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("xmls.xml");


?>
更新了显示的xml文件输出

This page contains the following errors:

error on line 2 at column 3431: Encoding error
Below is a rendering of the page up to the first error.

 Free shipping on orders worth $99 at LogoSportswearFree shipping on orders worth $99 at LogoSportswearhttp://www.logosoftwear.com/Save up to 75% on sale styles at loft.comGet a discount up to 75% on sale styles at loft.comhttp://www.loft.com/nfp-sale-cat-091312/cat1100006?supCat=catl00008&loc=hpa1&icid=hpa1_bestofsale_010715Free shipping on orders worth $200 or more at lechateau.comFree shipping on orders worth $200 or more at lechateau.comhttp://www.lechateau.com/style/index.jsp?storeId=334&locale=en_US&repriceOrder=trueSave up to 65% on outer wear at landsend.comGet a discount up to 65% on outer wear at landsend.comhttp://www.landsend.com/Free precious set with purchase worth $130 at loccitane.comFree precious set with purchase worth $130 at loccitane.com. Use the coupon code to avail the offer.http://usa.loccitane.com/precious-skin,82,1,65481,673013.htm#xcms_position=05_precious#xcms_campaign=shop_now_>Save 15% when you spend under $100 at kohls.comGet a discount of 15% when you spend under $100 at kohls.com. Use the coupon code to avail the offer.http://www.kohls.com/Save 10% on denim for the family at kmart.comGet a discount of 10% on denim for the family at kmart.comhttp://www.kmart.com/clothing-shoes-jewelry-specialty-shops-jeans-shop/b-1339616014?filter=storeOrigin&subCatView=true&adcell=hpmemberjeansSave up to 30% on dinner ware at kirklands.comGet a discount up to 30% on dinner ware at kirklands.comhttp://www.kirklands.com/category/Kitchen-Dining/Dinnerware/pc/2753/2681.uts?icid=hppromo5010115Save up to 50% on cargo pants at kingsizedirect.comGet a discount up to 50% on cargo pants at kingsizedirect.comhttp://www.kingsizedirect.com/Big-and-Tall-Cargo-Pants.aspx?DeptId=23955Free shipping on orders worth $75 at justmysizeFree shipping on orders worth $75 at justmysizehttp://www.hanes.com/justmysizeFree shipping site wide on all US orders at jomashop.comFree shipping site wide on all US orders at jomashop.com. Use the coupon code to avail the offer.http://www.jomashop.com/Save 20% on watches at jcpenney.comGet a discount of 20% on watches at jcpenney.com. Use the coupon code to avail the offer.http://www.jcpenney.com/jewelry-watches/watches/all-watches/cat.jump?id=cat100240089&deptId=dept20000020&subcatId=cat1003450012&N=1003070048&extDim=true&cm_re=S2-_-CAT-_-SPORT_WATCHESSave up to 60% on coats at jackthreads.comGet a discount up to 60% on coats at jackthreads.comhttps://www.jackthreads.com/sales/coats-are-up-to-60-off/15162
那我怎么才能得到这个


谢谢大家

谢谢大家帮助我,在谷歌的帮助下,我找到了答案

更新后的代码如下所示

$xml = new DomDocument("1.0","UTF-8");

$content = $xml->createElement("content");
$content = $xml->appendChild($content);

foreach($results as $result) {
    $item = $xml->createElement("item");

    $title = $xml->createElement("title",htmlspecialchars($result['title']));
    $title = $item->appendChild($title);

    $description = $xml->createElement("description",htmlspecialchars($result['content']));
    $description = $item->appendChild($description);

    $link = $xml->createElement("link",htmlspecialchars($result['url']));
    $link = $item->appendChild($link);

    $item = $content->appendChild($item);

}

$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("xmls.xml");

我只包含了xml文件。您只需创建与db的连接并使用此代码。

@SusanWilliams不要使用
htmlspecialchars
createElement
,请尝试对所有
createElement
使用
CreateCDATA节。在我看来,每个元素都有无效的xml数据,因此使用CDATA可以让您按原样存储数据,而不必转换所有数据。@prix..如果您能给我看一下itthank@prix的代码,那就太好了..我找到了一种方法,效果很好。但是我还有一个小小的疑问,如果我必须包括一个id,如图所示,然后是下一个值,就像这样,如果我想设置为所有的100,我怎么做呢。感谢您使用PHP和MySQL生成XML文件-最好创建文本节点(使用
DOMDocument::createTextNode()
)并将它们附加到元素节点。
This page contains the following errors:

error on line 2 at column 3431: Encoding error
Below is a rendering of the page up to the first error.

 Free shipping on orders worth $99 at LogoSportswearFree shipping on orders worth $99 at LogoSportswearhttp://www.logosoftwear.com/Save up to 75% on sale styles at loft.comGet a discount up to 75% on sale styles at loft.comhttp://www.loft.com/nfp-sale-cat-091312/cat1100006?supCat=catl00008&loc=hpa1&icid=hpa1_bestofsale_010715Free shipping on orders worth $200 or more at lechateau.comFree shipping on orders worth $200 or more at lechateau.comhttp://www.lechateau.com/style/index.jsp?storeId=334&locale=en_US&repriceOrder=trueSave up to 65% on outer wear at landsend.comGet a discount up to 65% on outer wear at landsend.comhttp://www.landsend.com/Free precious set with purchase worth $130 at loccitane.comFree precious set with purchase worth $130 at loccitane.com. Use the coupon code to avail the offer.http://usa.loccitane.com/precious-skin,82,1,65481,673013.htm#xcms_position=05_precious#xcms_campaign=shop_now_>Save 15% when you spend under $100 at kohls.comGet a discount of 15% when you spend under $100 at kohls.com. Use the coupon code to avail the offer.http://www.kohls.com/Save 10% on denim for the family at kmart.comGet a discount of 10% on denim for the family at kmart.comhttp://www.kmart.com/clothing-shoes-jewelry-specialty-shops-jeans-shop/b-1339616014?filter=storeOrigin&subCatView=true&adcell=hpmemberjeansSave up to 30% on dinner ware at kirklands.comGet a discount up to 30% on dinner ware at kirklands.comhttp://www.kirklands.com/category/Kitchen-Dining/Dinnerware/pc/2753/2681.uts?icid=hppromo5010115Save up to 50% on cargo pants at kingsizedirect.comGet a discount up to 50% on cargo pants at kingsizedirect.comhttp://www.kingsizedirect.com/Big-and-Tall-Cargo-Pants.aspx?DeptId=23955Free shipping on orders worth $75 at justmysizeFree shipping on orders worth $75 at justmysizehttp://www.hanes.com/justmysizeFree shipping site wide on all US orders at jomashop.comFree shipping site wide on all US orders at jomashop.com. Use the coupon code to avail the offer.http://www.jomashop.com/Save 20% on watches at jcpenney.comGet a discount of 20% on watches at jcpenney.com. Use the coupon code to avail the offer.http://www.jcpenney.com/jewelry-watches/watches/all-watches/cat.jump?id=cat100240089&deptId=dept20000020&subcatId=cat1003450012&N=1003070048&extDim=true&cm_re=S2-_-CAT-_-SPORT_WATCHESSave up to 60% on coats at jackthreads.comGet a discount up to 60% on coats at jackthreads.comhttps://www.jackthreads.com/sales/coats-are-up-to-60-off/15162
$xml = new DomDocument("1.0","UTF-8");

$content = $xml->createElement("content");
$content = $xml->appendChild($content);

foreach($results as $result) {
    $item = $xml->createElement("item");

    $title = $xml->createElement("title",htmlspecialchars($result['title']));
    $title = $item->appendChild($title);

    $description = $xml->createElement("description",htmlspecialchars($result['content']));
    $description = $item->appendChild($description);

    $link = $xml->createElement("link",htmlspecialchars($result['url']));
    $link = $item->appendChild($link);

    $item = $content->appendChild($item);

}

$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("xmls.xml");