drbae3964 2015-01-24 09:10
浏览 62

如何使用php和mysql创建xml文件? [重复]

I am trying to make a xml file from the data stored in my local db using php. But i am having a problem with it. as when I hard code the value I am able to get the xml file but when i am trying to call from the database i am not able to do it? so can some one help me out with it.

so here is the code that i wrote..

<?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");


?>

here Database::GetAll($sql) is a class that i have created, it executes the query and gets the table contents. the config file consists of all the database credentials and database.php is the file with the class of database.

The XML that i would want is

<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>

here the .... between the tags represent the datas retrieved from the database using mysql. var_dump($results) gives me all the three required values upto the 100th row.

and these are the errors that I am getting while using my code..

 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

updated xml file output as displayed..

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

so how can i get this?

thank you

</div>
  • 写回答

1条回答 默认 最新

  • duansai1314 2015-01-24 10:18
    关注

    Thank you all for helping me out, with all your help and a little help from google i found the answe for it..

    the updated code is as follow..

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

    I have only included the xml file. you can just create a connection with your db and use this code.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog