PHP使用DOMDocument 操作xml

  1 <?php

  2 class xmlMessage{

  3     protected $doc;

  4     protected $rootKey;//根节点

  5     public $xmlName;//xml文件名

  6 

  7     /*

  8     xmlMessage类构造函数

  9     @param string xmlName

 10     */

 11     public function  __construct($xmlName,$rootKey) {

 12         

 13         $this->xmlName=$xmlName;

 14         $this->rootKey = $rootKey;

 15         

 16         if(!file_exists($this->xmlName))

 17          {

 18             $this->doc = new DOMDocument('1.0', 'utf-8');

 19             $rootKey = $this->doc->createElement($this->rootKey);//create root key

 20             $this->rootKey = $this->doc->appendChild($rootKey);

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

 22          }else

 23          {

 24             $this->doc = new DOMDocument();            

 25             $this->doc->load($xmlName);

 26 

 27          }

 28          

 29             $this->doc->formatOutput = true;

 30     

 31         }

 32     

 33 

 34     /*

 35      创建子节点

 36      @param $childNode :要创建的子节点及属性$child_attr_array=null;    

 37      @param $parentNode:要创建的子节点的父节点及属性 $parent_attr_array=null;

 38     */

 39     public function createChildNode($childNode,$child_attr_array=null,$parentNode,$parent_attr_array=null)

 40     {

 41         $cnode = $this->doc->createElement($childNode);

 42 

 43         //获取要添加的子节点的父节点

 44         if($parent_attr_array!=null){

 45             foreach($parent_attr_array as $attr_key=>$attr_value)

 46             {

 47                 $cnode = $this->getNodeByAttr($parentNode,$attr_key,$attr_value)->appendChild($cnode);

 48             }

 49         }else

 50         {        //避免创建父节点即:在父节点下创建子节点    

 51                 $cnode = $this->doc->getElementsByTagName($parentNode)->item(0)->appendChild($cnode);

 52         }

 53 

 54         

 55            //设置子节点值

 56         //$cnode->nodeValue=$chileNodeValue;

 57 

 58         //设置属性

 59         if($child_attr_array!=null)

 60         {

 61             foreach($child_attr_array as $attr_key=>$attr_value)

 62             {

 63                 $cnode->setAttribute($attr_key,$attr_value);

 64             }

 65         }

 66 

 67 

 68 

 69     }

 70 

 71     /*

 72       获取具有某属性的节点

 73       @param 节点名$nodeName,及其属性和值$attrName,$attrValue

 74       @return DOMNode 

 75     */

 76     public function getNodeByAttr($nodeName,$attrName,$attrValue)

 77     {

 78         $nodeList = $this->doc->getElementsByTagName($nodeName);

 79 

 80         for($i=0; $i<$nodeList->length;$i++)

 81         {

 82             $attrList = $nodeList->item($i)->getAttributeNode($attrName);

 83             if($attrList->value == $attrValue)

 84             {

 85                 return $nodeList->item($i);

 86                 

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

 88             }

 89        

 90         }

 91         

 92     }

 93     

 94 

 95     //删除具有某个属性的节点

 96     public function delete_Node($nodeName,$attrName,$attrValue)

 97     {

 98         try

 99         {

100            $delete_Node = $this->getNodeByAttr($nodeName,$attrName,$attrValue);

101            $delete_Node->parentNode->removeChild($delete_Node);

102 

103         }catch(DOMException $e)

104         {

105             echo 'fail!';

106         }

107     }

108 

109 

110     //创建节点属性

111     public function createNodeAttribute($nodeName,$attr,$aValue)

112     {

113          $nodes = $this->doc->getElementsByTagName($nodeName);

114           for($i=0; $i<$nodes->length;$i++)

115         {

116           $nodeValue = $nodes->item($i)->nodeValue.'<br>';

117         }

118         

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

120 

121     }

122 

123     /*

124      保存xml

125     */

126     public function saveXml()

127         {

128            

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

130         }

131 

132 

133 

134    

135 

136 }

137 

138 //实例操作

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

140  

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

142 

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

144 

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

146   $myXml->createChildNode("浙江",'','国家',array('name'=>'英国'));

147 

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

149 

150 

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

152 

153   //$myXml->getNodeByAttr("国家",'name','英国');

154   //$myXml->delete_Node("历城",'city','济南');

155 

156 

157   $myXml->saveXml();

158 

159 ?>

 

你可能感兴趣的:(document)