【问题标题】:how to create xml file using php and mysql? [duplicate]如何使用 php 和 mysql 创建 xml 文件? [复制]
【发布时间】:2015-03-23 07:41:47
【问题描述】:

我正在尝试使用 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");


?>

这里 Database::GetAll($sql) 是我创建的一个类,它执行查询并获取表内容。 config 文件包含所有数据库凭据,database.php 是具有数据库类的文件。

我想要的 XML 是

<content>
<item>
<title>....</title>
<description>....</description>
<url>....</url>
</item>

<item>
<title>....</title>
<description>....</description>
<url>....</url>
</item>

<item>
<title>....</title>
<description>....</description>
<url>....</url>
</item>

...till 100 values..
</content>

这里标签之间的....表示使用mysql从数据库中检索到的数据。 var_dump($results) 为我提供了所有三个必需的值,直到第 100 行。

这些是我在使用我的代码时遇到的错误..

 Warning: DOMDocument::createElement(): unterminated entity reference loc=hpa1&icid=hpa1_bestofsale_010715 in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference locale=en_US&repriceOrder=true in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference subCatView=true&adcell=hpmemberjeans in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference deptId=dept20000020&subcatId=cat1003450012&N=1003070048&extDim=true&cm_re=S2-_-CAT-_-SPORT_WATCHES in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference ab=HP_BSpot_B4_40OffFootwear in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference pid=8672,8541,8532,8699,107705,8664,8806,8539,8808,8540,8826,8828,8542,104632,107703,8827&sortExpression=manual&heig in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference cm_sp=LN-_-Save+Up+to+75%25+Off+Select+Items++-_-Up+to+50%25+Off+Select+Body+Care&cp=4090263.46772866 in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference CP=ILC-FLASH:20offShoes&sortmfr=N in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference pid=8672,8541,8532,8699,107705,8664,8806,8539,8808,8540,8826,8828,8542,104632,107703,8827&sortExpression=manual&heig in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference so=2 in C:\xampp\htdocs\practice\d\xmls.php on line 23

Warning: DOMDocument::createElement(): unterminated entity reference node=10470257011&smid=ATVPDKIKX0DER&pf_rd_m=ATVPDKIKX0DER&pf_rd_t=701&pf_rd_s=center-3&pf_rd_r=0ZC491KTZNEXXHNV96NM&pf_rd_i=30&pf in C:\xampp\htdocs\practice\d\xmls.php on line 23

更新后的 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

那么我怎样才能得到这个?

谢谢

【问题讨论】:

  • @SusanWilliams 而不是使用htmlspecialcharscreateElement,尝试使用createCDATASection 代替所有createElement。在我看来,您对每个元素都有无效的 xml 数据,因此使用 CDATA 将允许您按原样存储数据,而不必将其全部隐藏。
  • @prix..如果你能给我看它的代码就太好了
  • 感谢@prix..我找到了一种方法,效果很好。但我还有一点疑问..如果我必须包含一个 id,如 所示,然后 为下一个值。 .like this 如果我想设置为所有 100 我该怎么做。谢谢
  • 使用 PHP 和 MySQL 生成 XML 文件 - kvcodes.com/2017/03/generate-xml-file-using-php-mysql

标签: php mysql xml database


【解决方案1】:

谢谢大家帮助我,在你们的帮助和谷歌的一点帮助下,我找到了答案..

更新后的代码如下..

$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 文件。您可以创建与您的数据库的连接并使用此代码。

【讨论】:

  • 最好创建文本节点(使用DOMDocument::createTextNode())并将它们附加到您的元素节点。
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 2011-07-18
  • 2010-11-13
相关资源
最近更新 更多