php 生成和下载xml文件

方法一
$doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $rootEle = $doc->createElement('root'); $doc->appendchild($rootEle); $descriptionEle = $doc->createElement('description'); $rootEle->appendChild($descriptionEle); $couponNameEle = $doc->createElement('couponName'); $couponDescriptionEle = $doc->createElement('couponDescription'); $couponNameEle->appendChild($doc->createTextNode('jiuzhe')); $couponDescriptionEle->appendchild($doc->createTextNode('jiuzhedescription')); $descriptionEle->appendchild($couponNameEle); $descriptionEle->appendChild($couponDescriptionEle); $detailEle = $doc->createElement('detail'); $rootEle->appendchild($detailEle); $itemEle = $doc->createElement('item'); $detailEle->appendChild($itemEle); $codeEle = $doc->createElement('code'); $codeEle->appendchild($doc->createTextNode('666666')); $itemEle->appendchild($codeEle); $itemEle = $doc->createElement('item'); $detailEle->appendChild($itemEle); $codeEle = $doc->createElement('code'); $codeEle->appendchild($doc->createTextNode('999999')); $itemEle->appendchild($codeEle); $doc->save('/home/susie/workspace/' . 'offer-coupon.xml');
<root>
  <
description>
    <
couponName>jiuzhe</couponName>
    <
couponDescription>jiuzhedescription</couponDescription>
  </
description>
  <
detail>
    <
item>
      <
code>666666</code>
    </
item>
    <
item>
      <
code>999999</code>
    </
item>
  </
detail>
</
root>

 方法二:

$lineFeed = "\n";

$xml = '<?xml version="1.0" encoding="utf-8"?>' . $lineFeed;
$xml .= '<root>' . $lineFeed;
$xml .= '<description>' . $lineFeed;
$xml .= '<couponName>' . "1111" . '</couponName>' . $lineFeed;
$xml .= '<couponDescription>' . "222" . '</couponDescription>' . $lineFeed;
$xml .= '<quantity>' . "3333" . '</quantity>' . $lineFeed;
$xml .= '</description>' . $lineFeed;
$xml .= '<codes>' . $lineFeed;
$xml .= '<code>' . "codecode" . '</code>' . $lineFeed;
$xml .= '</codes>' . $lineFeed;
$xml .= '</root>' . $lineFeed;

 

下载xml文件

header("Content-Type:text/xml charset=UTF-8");
header("Content-Disposition:attachment; filename=offer-coupon.xml");
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
$fp = fopen('php://output', 'a');

fwrite($fp, $xml);

fclose($fp);
die();

 

 

 

参考网站: http://yedward.net/?id=21

posted @ 2016-04-11 14:23  yanying12138  阅读(401)  评论(0编辑  收藏  举报