您的位置:首页 > 编程语言 > PHP开发

PHP使用DOMDocument 操作xml

2013-06-03 11:23 302 查看
<?php
class xmlMessage{
protected $doc;
protected $rootKey;//根节点
public $xmlName;//xml文件名

/*
xmlMessage类构造函数
@param string xmlName
*/
public function  __construct($xmlName,$rootKey) {

$this->xmlName=$xmlName;
$this->rootKey = $rootKey;

if(!file_exists($this->xmlName))
{
$this->doc = new DOMDocument('1.0', 'utf-8');
$rootKey = $this->doc->createElement($this->rootKey);//create root key
$this->rootKey = $this->doc->appendChild($rootKey);
$this->doc->save($this->xmlName);
}else
{
$this->doc = new DOMDocument();
$this->doc->load($xmlName);

}

$this->doc->formatOutput = true;

}

/*
创建子节点
@param $childNode :要创建的子节点及属性$child_attr_array=null;
@param $parentNode:要创建的子节点的父节点及属性 $parent_attr_array=null;
*/
public function createChildNode($childNode,$child_attr_array=null,$parentNode,$parent_attr_array=null)
{
$cnode = $this->doc->createElement($childNode);

//获取要添加的子节点的父节点
if($parent_attr_array!=null){
foreach($parent_attr_array as $attr_key=>$attr_value)
{
$cnode = $this->getNodeByAttr($parentNode,$attr_key,$attr_value)->appendChild($cnode);
}
}else
{        //避免创建父节点即:在父节点下创建子节点
$cnode = $this->doc->getElementsByTagName($parentNode)->item(0)->appendChild($cnode);
}

//设置子节点值
//$cnode->nodeValue=$chileNodeValue;

//设置属性
if($child_attr_array!=null)
{
foreach($child_attr_array as $attr_key=>$attr_value)
{
$cnode->setAttribute($attr_key,$attr_value);
}
}

}

/*
获取具有某属性的节点
@param 节点名$nodeName,及其属性和值$attrName,$attrValue
@return DOMNode
*/
public function getNodeByAttr($nodeName,$attrName,$attrValue)
{
$nodeList = $this->doc->getElementsByTagName($nodeName);

for($i=0; $i<$nodeList->length;$i++)
{
$attrList = $nodeList->item($i)->getAttributeNode($attrName);
if($attrList->value == $attrValue)
{
return $nodeList->item($i);

//echo 'okkkkkk'.'<br>';;
}

}

}

//删除具有某个属性的节点
public function delete_Node($nodeName,$attrName,$attrValue)
{
try
{
$delete_Node = $this->getNodeByAttr($nodeName,$attrName,$attrValue);
$delete_Node->parentNode->removeChild($delete_Node);

}catch(DOMException $e)
{
echo 'fail!';
}
}

//创建节点属性
public function createNodeAttribute($nodeName,$attr,$aValue)
{
$nodes = $this->doc->getElementsByTagName($nodeName);
for($i=0; $i<$nodes->length;$i++)
{
$nodeValue = $nodes->item($i)->nodeValue.'<br>';
}

//$node->item[0]->setAttribute($attr,$aValue);

}

/*
保存xml
*/
public function saveXml()
{

$this->doc->save($this -> xmlName);
}

}

//实例操作
$myXml = new xmlMessage("opera.xml","世界");//创建一个以"世界"为根节点的xml文件

$myXml->createChildNode("国家",array('name'=>'中国','人口'=>'1亿'),'世界');

$myXml->createChildNode("国家",array('name'=>'英国','人口'=>'50millon'),'世界');

$myXml->createChildNode("山东",array('city'=>'济南','note'=>'省会'),'国家',array('name'=>'英国'));
$myXml->createChildNode("浙江",'','国家',array('name'=>'英国'));

//$myXml->createChildNode("历城",array('city'=>'济南','note'=>'区'),'山东');

//$myXml->createNodeAttribute("国家","人口",125);

//$myXml->getNodeByAttr("国家",'name','英国');
//$myXml->delete_Node("历城",'city','济南');

$myXml->saveXml();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: