Create XML file

WBOY
WBOYOriginal
2016-07-29 09:12:211056browse

Use dom to create xml files

<?php 
/*用dom创xml文档
方法:从最小的节点往外建*/

$dom=new Domdocument(&#39;1.0&#39;,&#39;utf-8&#39;);
// 1先建文本节点,"天龙八部"
$comic=$dom->createTextNode('喜欢就是喜欢');
// 2再建name节点
$name=$dom->createElement('name');
// 3文本 加进 name节点
$name->appendChild($comic);
// 4创CDATA,准备放5
$cdata=$dom->createCDATASection('这是cdata,说明这个片子是bl向的233');
// 5创intro节点,同2
$intro=$dom->createElement('intro');


// 6把cdata放进intro
$intro->appendChild($cdata);
// 7创建goods节点
$goods=$dom->createElement('goods');
//把nameintro都放进goods
$goods->appendChild($name);

$goods->appendChild($intro);

// 8创建goodsid属性
$id=$dom->createAttribute('goods_id');
$id->value='g01';
// 9把属性-》goods节点
$goods->appendChild($id);
// 10
$store=$dom->createElement('store');
$store->appendChild($goods);
$dom->appendChild($store);

// 直接输出
header('content-type:text/xml');
echo $dom->savexml();

// 保存
// $dom->save('try.xml');

The direct output is on the web page

There is a convenient way to create it later, write a class or use simplexml

The above introduces the creation of XML files, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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