您当前的位置: 首页 > 学无止境 > XML与XSLT 网站首页XML与XSLT
3.xml的创建
发布时间:2017-04-30 12:42:10编辑:雪饮阅读()
<?php
/*
待创建xml格式
<animal>
<name>dog</name>
<color>yellow</color>
<age>2</age>
</animal>
*/
$dom=new DOMDocument('1.0','utf-8');
//创建元素节点
$animal=$dom->createElement("animal");
$name=$dom->createElement("name");
$color=$dom->createElement("color");
$age=$dom->createElement("age");
//创建属性节点(方式一)
$weight_txt=$dom->createTextNode("50");
$weight=$dom->createAttribute('weight');
$weight->appendChild($weight_txt);
$name->appendChild($weight);
//创建属性节点(方式二)
$name->setAttribute('height',20);
//创建文本节点
$name_text=$dom->createTextNode("dog");
$color_txt=$dom->createTextNode("yellow");
$age_txt=$dom->createTextNode("2");
//创建cdata节点
$intro_txt=$dom->createCDATASection('very<g>ood');
$intro=$dom->createElement('intro');
$intro->appendChild($intro_txt);
//节点挂载
$name->appendChild($name_text);
$color->appendChild($color_txt);
$age->appendChild($age_txt);
$animal->appendChild($name);
$animal->appendChild($color);
$animal->appendChild($age);
$animal->appendChild($intro);
$dom->appendChild($animal);
//输出xml信息
//header("content-type:text/xml;charset=utf-8");
//echo $dom->saveXML();
//生成xml文件
//xml格式化开启
$dom->formatOutput=true;
$dom->save("./08.xml");
?>
关键字词:php,xml,创建
上一篇:2.php解析xml
下一篇:4.节点的删除与替换