PHP的SimpleXML保存XML文档

一 代码

11.xml
<?xml version="1.0" encoding="gb2312"?>
<object name="商品">
	<book>
		<computerbook type="PHP程序员必备工具">PHP函数参考大全</computerbook>
	</book>
</object>
 
index.php
<?php
/*  创建SimpleXML对象  */
$xml = simplexml_load_file('11.xml');
/*  修改XML文档内容  */
$xml->book->computerbook['type'] = iconv('gb2312','utf-8','PHP程序员必备工具');
$xml->book->computerbook = iconv('gb2312','utf-8','PHP函数参考大全');
/*  格式化对象$xml  */
$modi = $xml->asXML();
/*  将对象保存到10.xml文档中  */
file_put_contents('10.xml',$modi);
/*  重新读取xml文档  */ 
$str = file_get_contents('10.xml');
/*  输出修改后的文档内容  */
echo $str;
?>
 
二 运行结果
PHP函数参考大全
10.xml的内容:

 

猜你喜欢

转载自cakin24.iteye.com/blog/2378828