php操作xml类 _xml = new DOMDocument('1.0', 'utf-8');$this-_xml-formatOutput = true;}/* *设置XML版本 *@param $version string */public function setVersion($version) {$this-_xml-xmlVersion = $version;}/* *设置版本 *@param $encoding
_xml = new DOMDocument('1.0', 'utf-8'); $this->_xml->formatOutput = true; } /* *设置XML版本 *@param $version string */ public function setVersion($version) { $this->_xml->xmlVersion = $version; } /* *设置版本 *@param $encoding string */ public function setEncoding($encoding) { $this->_xml->encoding = $encoding; } /* *设置standalone *@param $standalone boolean,默认false */ public function setStandalone($standalone = false) { $this->_xml->xmlStandalone = $standalone; } /* *加载xml *@param $path string,已'/'结尾 *@param $name string,包含扩展名的文件名 */ public function load($path, $name) { $this->_xml->load($path.$name); } /* *添加节点 *@param $node string,待添加节点 *@param $prev string/array/null,上级节点 *@param $attr array,节点属性 *@param $text string,节点文本 */ public function addNode($node, $prev = null, $attr = Array(), $text = '') { $node_object = $this->_xml->createElement($node); if ('' != $text) { $text_node = $this->_xml->createTextNode($text); $node_object->appendChild($text_node); } if(0 != count($attr)) { $this->addAttr($attr, $node_object); } if (null == $prev) { $this->_xml->appendChild($node_object); } if (is_string($prev) && null != $prev) { $list = $this->_xml->getElementsByTagName($prev); for($i = 0; $i < count($list); $i++) { $list->item($i)->appendChild($node); } } if (is_array($prev)) { foreach($prev as $k => $v) { $list = $this->_xml->getElementsByTagName($k); $list->item($v)->appendChild($node); } } } /* *添加属性 *@param $attr array *@param $node object/array */ public function addAttr($attr, $node) { if (is_object($node)) { foreach($attr as $attr_name => $attr_value) { $tmp_attr = $this->_xml->createAttribue($attr_name); $tmp_attr->value = $attr_value; $node->appendChild($tmp_attr); } } if (is_array($node)) { foreach($node as $node_name => $node_index) { $node_collect = $this->_xml->getElementsByTagName($node_name); if (is_string($node_index)) { foreach($node_collect => $node_object) { foreach($attr as $attr_name => $attr_value) { $tmp_attr = $this->_xml->createAttribue($attr_name); $tmp_attr->value = $attr_value; $node_object->appendChild($tmp_attr); } } } if (is_array($node_index)) { for($index = 0; $index < count($nodex_indexs); $index++) { foreach($attr as $attr_name => $attr_value) { $tmp_attr = $this->_xml->createAttribue($attr_name); $tmp_attr->value = $attr_value; $node_collect->item($index)->appendChild($tmp_attr); } } } } } } /* *删除节点 *@param $node object/array */ public function deleteNode($node) { if(is_object($node)){ $this->_xml->removeChild($node); } if(is_array($node)){ foreach($node as $node_name => $node_index) { $node_collect = document.getElementsByTagName($node_name); if (is_string($node_index)) { foreach($node_collect as $node_object) { $this->_xml->removeChild($node_object); } } if (is_array($node_index)) { foreach($node_index as $node_collect_index) { $this->_xml->remove($node_collect->item($node_collect_index)); } } } } } /* *删除属性 *@param $attr array *@param $node object/array */ public function deleteAttr($attr, $node) { if(is_object($node)) { foreach($attr as $attr_name) { $node_object->removeAttribute($attr_name); } } if(is_array($node)) { foreach($node as $node_name => $node_index) { $node_collect = $this->_xml->getElementsByTagName($node_name); if (is_string($node_index)) { foreach($node_collect => $node_object) { foreach($attr as $attr_name) { $node_object->removeAttribute($attr_name); } } } if (is_array($node_index)) { for($index = 0; $index < count($nodex_indexs); $index++) { foreach($attr as $attr_name) { $node_collect->item($index)->removeAttribute($attr_name); } } } } } } /* *保存 *@param $path string,已'/'结尾 *@param $name string,包含扩展名的文件名 */ public function save($path, $name) { $this->_xml->save($path.$name); $this->_xml = new DOMDocument(); } }