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

转载于:https://www.cnblogs.com/Brittany-yan/p/5378276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值