方法一
$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

php 生成和下载xml文件的更多相关文章

  1. 下载Xml文件方法

    #region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...

  2. C#动态生成图书信息XML文件

    通过C#动态生成图书信息XML文件,下面有个不错的示例,需要的朋友可以参考下 通过C#动态生成图书信息XML文件(Books.xml),文件如下: 复制代码代码如下: <?xml version ...

  3. dom4j生成和解析xml文件

    dom4j生成和解析xml文件 要生成和解析如下格式的xml文件: <?xml version="1.0" encoding="UTF-8"?> & ...

  4. 108.生成和下载csv文件

    生成CSV文件 有时候我们做的网站,需要将一些数据,生成一个csv文件返回浏览器,并且是作为附件的形式下载下来. 生成小的csv文件: 生成一个小的csv文件,我们用Python内置的csv模块来处理 ...

  5. 【转】Java生成plist下载ipa文件

    我们在上传ipa想要安装的时候必须要通过plist文件去下载,并且还要遵循 itms-services协议. 意思就是,第一步我们要生成一个plist文件, 第二步生成一个html文件,用来指向pli ...

  6. C#生成、解析xml文件以及处理报错原因

    转载自:http://blog.csdn.net/lilinoscar/article/details/21027319 简单的介绍一下生成XML文件以及解析,因为有些数据不一定放到数据库,减少链接数 ...

  7. C++生成和解析XML文件

    1.xml 指可扩展标记语言(EXtensible Markup Language) 2.xml 是一种标记语言,类似html 3.xml 的设计宗旨是传输数据,而非显示数据 4.xml 标签没有被预 ...

  8. SSM 生成mapper中xml文件:未能解析映射资源:“文件嵌套异常

    错误日记我就网上随便找个贴着: 错误一: org.springframework.beans.factory.BeanCreationException: Error creating bean wi ...

  9. C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库

    using FtpLib; using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...

随机推荐

  1. Lintcode--006(交叉字符串)

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  2. 安卓仿制新浪微博(一)之OAuth2授权接口

    这里需要用到请求授权(authorize)以及获取授权(access_token) 第一步: 将新浪的sdk放在src/libs下面 二: //创建方法实现authorize public void ...

  3. pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序

    pywin32 安装错误 ImportError: DLL load failed:  不是有效的 Win32 应用程序. 发现是因为没有制定Pywin32的dll所致,我们在用Pywin32开发时, ...

  4. 微软CEO史蒂夫·鲍尔默(Steve Ballmer)在12个月内退休

    Microsoft CEO Steve Ballmer to retire within 12 months Aug. 23, 2013 Board of directors initiates su ...

  5. library cache: mutex X

    我们先来看看 library cache: mutex X . 是个什么东西 The library cache mutex is acquired for similar purposes that ...

  6. WPF - ViewModle中关闭Window

    在Binding close event时候,需要从ViewModel关闭Window. 一个很简洁的解决方案就是,将Window 当做CommandParameter传过去. Command=&qu ...

  7. pyqt例子搜索文本

    #!/usr/bin/env python #-*- coding:utf-8 -*- import sip sip.setapi('QString', 2) sip.setapi('QVariant ...

  8. Phoenix二级索引(Secondary Indexing)的使用

    摘要 HBase只提供了一个基于字典排序的主键索引,在查询中你只能通过行键查询或扫描全表来获取数据,使用Phoenix提供的二级索引,可以避免在查询数据时全表扫描,提高查过性能,提升查询效率   测试 ...

  9. C++关键字之const

    C++的const关键字一直让我很乱,这里把const关键字在程序中常用的三个地方分别总结出来: 1.  通过指针或引用访问普通变量 2.  通过对象调用成员函数,通过成员函数访问成员变量 3.  通 ...

  10. [Node.js] Using ES6 and beyond with Node.js

    If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...