PHP使用 DOMDocument创建和解析xml文件

<!-- DOMDocument生成XML文件 -->

<?php

//声明一个DOMDocument对象

$_doc=new DOMDocument('1.0', 'utf-8');

//使用XML标准化输出

$_doc->formatOutput=true;

//使用createelement创建标签

$_root=$_doc->createElement('root');

$_version=$_doc->createElement('version');

//创建version标签字符串(值)

$_versionTextNode=$_doc->createTextNode('1.0');

//将该字符串插入version标签中

$_version->appendChild($_versionTextNode);

//将version标签插入root标签中

$_root->appendChild($_version);

//将root标签插入文件中

$_doc->appendChild($_root);

//生成XML文件

$_doc->save('test1.xml');

?>

<!-- DOMDocument生成XML文件 -->

<?php

//声明一个DOMDocument对象

$_doc=new DOMDocument('1.0', 'utf-8');

//使用XML标准化输出

$_doc->formatOutput=true;

//使用createelement创建标签

$_root=$_doc->createElement('root');

$_version=$_doc->createElement('version');

//创建version标签字符串(值)

$_versionTextNode=$_doc->createTextNode('1.0');

//将该字符串插入version标签中

$_version->appendChild($_versionTextNode);

//将version标签插入root标签中

$_root->appendChild($_version);

//将root标签插入文件中

$_doc->appendChild($_root);

//生成XML文件

$_doc->save('test2.xml');

?>