• 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 = "
    ";
    
    $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

  • 相关阅读:
    深度学习优化方法比较
    调参
    Numpy/Pytorch之数据类型与强制转换
    numpy:维度问题
    js模板引擎-juicer
    js模板引擎-腾讯artTemplate 简洁语法例子
    canva绘制时钟
    js中的break ,continue, return
    JavaScript奇技淫巧44招
    数据类型
  • 原文地址:https://www.cnblogs.com/Brittany-yan/p/5378276.html
Copyright © 2020-2023  润新知