PHP输出生成XML文件实例程序
php output xml
Generate xml file
在php中输出生成xml文件的方法有很多,有直接用header输入,也有使用DomDocument与SimpleXML实现创建xml文档的.
方法一,代码如下:
function xml_out($content, $charset = 'utf-8') { @header("Expires: -1"); @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE); @header("Pragma: no-cache"); @header("Content-type: application/xml; charset=$charset"); echo '<' . "?xml version=\"1.0\" encoding=\"$charset\"?>\n"; echo "<root><![CDATA[" . trim($content) . "]]></root>"; exit(); }
Copy after login
方法二,代码如下:
<?php header("Content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\""; echo "<users><user><name>小小菜鸟</name><age>24</age><sex>男</sex></user><user><name>艳艳</name><age>23</age><sex>女</sex></user></users>"; ?>
Copy after login
方法三:使用DomDocument生成XML文件
创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法,代码如下:
<?PHP $data_array = array( array( 'title' => 'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ), array( 'title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11', ) ); // 属性数组 $attribute_array = array( 'title' => array( 'size' => 1 ) ); // 创建一个XML文档并设置XML版本和编码。。 $dom=new DomDocument('1.0', 'utf-8'); // 创建根节点 $article = $dom->createElement('article'); $dom->appendchild($article); foreach ($data_array as $data) { $item = $dom->createElement('item'); $article->appendchild($item); create_item($dom, $item, $data, $attribute_array); } echo $dom->saveXML(); function create_item($dom, $item, $data, $attribute) { if (is_array($data)) { foreach ($data as $key => $val) { // 创建元素 $$key = $dom->createElement($key); $item->appendchild($$key); // 创建元素值 $text = $dom->createTextNode($val); $$key->appendchild($text); if (isset($attribute[$key])) { // 如果此字段存在相关属性需要设置 foreach ($attribute[$key] as $akey => $row) { // 创建属性节点 $$akey = $dom->createAttribute($akey); $$key->appendchild($$akey); // 创建属性值节点 $aval = $dom->createTextNode($row); $$akey->appendChild($aval); } } // end if } } // end if } // end function ?>
Copy after login
方法四:SimpleXML输入xml格式编码
SimpleXML作为PHP核心的组成部分,可以把XML转换为对象,但是有时候,我需要对输出的xml格式设置编码,代码如下:
$XML = new SimpleXMLElement("<foo />"); echo($XML->asXML()); //输出结果:<?xml version="1.0"?> <foo/> //如果想输出,代码如下:<?xml version="1.0" encoding="UTF-8"?> <foo/>
Copy after login
文章网址:
随意转载^^但请附上教程地址。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
1 months ago
By DDD
R.E.P.O. Best Graphic Settings
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1359
52

