标签归档:XML

利用SimpleXML 函数 生成XML文件

直接贴例子

 */

$data = array(
	array('ID' => 1, 'Title' => '程序设计', 'Desc' => '编程珠玑', 'Url' => 'Http://www.chengxusheji.com'),
	array('ID' => 2, 'Title' => 'Redisfans', 'Desc' => 'Redis爱好者', 'Url' => 'Http://www.redisfans.com'),
	array('ID' => 3, 'Title' => 'PHP之路', 'Desc' => 'PHP学习之路', 'Url' => 'Http://www.phpzl.com')	
);

$xmlString =<<<XML
<?xml version='1.0' encoding='utf-8'?>
<document>
</document>
XML;

$xml = simplexml_load_string($xmlString);

$xml->addChild('webSite', 'http://www.chengxusheji.com');//给 SimpleXML 元素添加一个子元素。
$xml->addChild('webMaster', '40285090@qq.com');
$xml->addChild('updatePeri', '10000');

//元素属性数组
$attributeArray = array(
    'Title' => array(
    'author' => 'siyuantlw'
    )
);

foreach($data as $key => $value) {

	$item = $xml->addChild('item');

	foreach ($value as $k => $val) {
		$node = $item->addChild($k, $val);

		if(isset($attributeArray[$k]) && is_array($attributeArray[$k])) {
			foreach ($attributeArray[$k] as $aKey => $aVal) {
				$node->addAttribute($aKey, $aVal);//给 SimpleXML 元素添加一个属性
			}
		}
	}
}

//输出
header("Content-type: text/xml");
echo $xml->asXml();//从 SimpleXML 元素获取 XML 字符串

//保存文件
//$xml->asXml("filepath/video.xml");

去下例子吧,wp编辑器过滤掉好多东西~
例子文件下载:http://pan.baidu.com/s/1c0jAsgg
提取密码:9j1i