<?php/** * filename: domEx.php * * Editor: richard_ma * * Date: 2007-07-27 * * Description: * 利用php DOM函数库创建xml文档 */// 设置Http头属性为xmlheader("Content-Type:text/xml");/** * 创建文档对象及根节点 */// 创建DOM对象,xml版本为1.0编码方式为UTF-8$dom = new DOMDocument('1.0', 'UTF-8');// 创建节点$response = $dom->createElement('rootNode');// 将节点作为子节点加入xml文档中$dom->appendChild($response);/** * 创建属性 */// 创建属性节点$resAttribute = $dom->createAttribute('attrNode');// 插入属性节点$response->appendChild($resAttribute);// 创建属性值$attrValue = $dom->createTextNode('attrValue');// 插入属性值$resAttribute->appendChild($attrValue);/** * 创建标签内容 */// 创建文字节点$resContent = $dom->createTextNode('textNode');// 将文字节点作为子节点加入根节点中$response->appendChild($resContent);// 导出xml字符串$xmlStr = $dom->saveXML();// 输出xml字符串echo $xmlStr;?>