PHP使用DOM XML操作XML[总结]-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

PHP使用DOM XML操作XML[总结]

简介:

1、前言

  XML树状层次结构鲜明,非常适合作为配置文件。PHP中可以使用DOM XML来操作XML。本文总结一下PHP使用DOM XML创建、添加节点、查询XML文件。

2、使用DOM XML

  XML中节点分为元素和文本,DOMDocument类型是文档类型,提供了操作元素和文本的成员函数和属性。

DOMDocument类如下:

复制代码
DOMDocument extends DOMNode {
/* Properties */
readonly public string $actualEncoding ;
readonly public DOMConfiguration $config ;
readonly public DOMDocumentType $doctype ;
readonly public DOMElement $documentElement ;
public string $documentURI ;
public string $encoding ;
public bool $formatOutput ;
readonly public DOMImplementation $implementation ;
public bool $preserveWhiteSpace =true ;
public bool $recover ;
public bool $resolveExternals ;
public bool $standalone ;
public bool $strictErrorChecking =true ;
public bool $substituteEntities ;
public bool $validateOnParse =false ;
public string $version ;
readonly public string $xmlEncoding ;
public bool $xmlStandalone ;
public string $xmlVersion ;
/* Methods */
__construct ([ string $version [, string $encoding ]] )
DOMAttr createAttribute ( string $name )
DOMAttr createAttributeNS ( string $namespaceURI , string $qualifiedName )
DOMCDATASection createCDATASection ( string $data )
DOMComment createComment ( string $data )
DOMDocumentFragment createDocumentFragment ( void )
DOMElement createElement ( string $name [, string $value ] )
DOMElement createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )
DOMEntityReference createEntityReference ( string $name )
DOMProcessingInstruction createProcessingInstruction ( string $target [, string $data ] )
DOMText createTextNode ( string $content )
DOMElement getElementById ( string $elementId )
DOMNodeList getElementsByTagName ( string $name )
DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
DOMNode importNode ( DOMNode $importedNode [, bool $deep ] )
mixed load ( string $filename [, int $options=0 ] )
bool loadHTML ( string $source )
bool loadHTMLFile ( string $filename )
mixed loadXML ( string $source [, int $options=0 ] )
void normalizeDocument ( void )
bool registerNodeClass ( string $baseclass , string $extendedclass )
bool relaxNGValidate ( string $filename )
bool relaxNGValidateSource ( string $source )
int save ( string $filename [, int $options ] )
string saveHTML ( void )
int saveHTMLFile ( string $filename )
string saveXML ([ DOMNode $node [, int $options ]] )
bool schemaValidate ( string $filename )
bool schemaValidateSource ( string $source )
bool validate ( void )
int xinclude ([ int $options ] )
/* Inherited methods */
DOMNode DOMNode::appendChild ( DOMNode $newnode )
DOMNode DOMNode::cloneNode ([ bool $deep ] )
bool DOMNode::hasAttributes ( void )
bool DOMNode::hasChildNodes ( void )
DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool DOMNode::isDefaultNamespace ( string $namespaceURI )
bool DOMNode::isSameNode ( DOMNode $node )
bool DOMNode::isSupported ( string $feature , string $version )
string DOMNode::lookupNamespaceURI ( string $prefix )
string DOMNode::lookupPrefix ( string $namespaceURI )
void DOMNode::normalize ( void )
DOMNode DOMNode::removeChild ( DOMNode $oldnode )
DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}
复制代码

元素节点类DOMElement类型定义如下:

复制代码
 1 DOMElement extends DOMNode {
 2 /* Properties */
 3 readonly public bool $schemaTypeInfo ;
 4 readonly public string $tagName ;
 5 /* Methods */
 6 __construct ( string $name [, string $value [, string $namespaceURI ]] )
 7 string getAttribute ( string $name )
 8 DOMAttr getAttributeNode ( string $name )
 9 DOMAttr getAttributeNodeNS ( string $namespaceURI , string $localName )
10 string getAttributeNS ( string $namespaceURI , string $localName )
11 DOMNodeList getElementsByTagName ( string $name )
12 DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
13 bool hasAttribute ( string $name )
14 bool hasAttributeNS ( string $namespaceURI , string $localName )
15 bool removeAttribute ( string $name )
16 bool removeAttributeNode ( DOMAttr $oldnode )
17 bool removeAttributeNS ( string $namespaceURI , string $localName )
18 DOMAttr setAttribute ( string $name , string $value )
19 DOMAttr setAttributeNode ( DOMAttr $attr )
20 DOMAttr setAttributeNodeNS ( DOMAttr $attr )
21 void setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )
22 void setIdAttribute ( string $name , bool $isId )
23 void setIdAttributeNode ( DOMAttr $attr , bool $isId )
24 void setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )
25 /* Inherited methods */
26 DOMNode DOMNode::appendChild ( DOMNode $newnode )
27 DOMNode DOMNode::cloneNode ([ bool $deep ] )
28 bool DOMNode::hasAttributes ( void )
29 bool DOMNode::hasChildNodes ( void )
30 DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
31 bool DOMNode::isDefaultNamespace ( string $namespaceURI )
32 bool DOMNode::isSameNode ( DOMNode $node )
33 bool DOMNode::isSupported ( string $feature , string $version )
34 string DOMNode::lookupNamespaceURI ( string $prefix )
35 string DOMNode::lookupPrefix ( string $namespaceURI )
36 void DOMNode::normalize ( void )
37 DOMNode DOMNode::removeChild ( DOMNode $oldnode )
38 DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
39 }
复制代码

文本类型DOMText定义如下:

复制代码
DOMText extends DOMCharacterData {
/* Properties */
readonly public string $wholeText ;
/* Methods */
__construct ([ string $value ] )
bool isWhitespaceInElementContent ( void )
DOMText splitText ( int $offset )
/* Inherited methods */
void DOMCharacterData::appendData ( string $data )
void DOMCharacterData::deleteData ( int $offset , int $count )
void DOMCharacterData::insertData ( int $offset , string $data )
void DOMCharacterData::replaceData ( int $offset , int $count , string $data )
string DOMCharacterData::substringData ( int $offset , int $count )
}
复制代码

DOMNode节点定义如下:

复制代码
DOMNode {
/* Properties */
public readonly string $nodeName ;
public string $nodeValue ;
public readonly int $nodeType ;
public readonly DOMNode $parentNode ;
public readonly DOMNodeList $childNodes ;
public readonly DOMNode $firstChild ;
public readonly DOMNode $lastChild ;
public readonly DOMNode $previousSibling ;
public readonly DOMNode $nextSibling ;
public readonly DOMNamedNodeMap $attributes ;
public readonly DOMDocument $ownerDocument ;
public readonly string $namespaceURI ;
public string $prefix ;
public readonly string $localName ;
public readonly string $baseURI ;
public string $textContent ;
/* Methods */
DOMNode appendChild ( DOMNode $newnode )
DOMNode cloneNode ([ bool $deep ] )
bool hasAttributes ( void )
bool hasChildNodes ( void )
DOMNode insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool isDefaultNamespace ( string $namespaceURI )
bool isSameNode ( DOMNode $node )
bool isSupported ( string $feature , string $version )
string lookupNamespaceURI ( string $prefix )
string lookupPrefix ( string $namespaceURI )
void normalize ( void )
DOMNode removeChild ( DOMNode $oldnode )
DOMNode replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}
复制代码

3、测试程序

复制代码
  1 <?php
  2 
  3 const INDEX_FILE_NAME = "student_file_index.xml";
  4 
  5 //文件索引类
  6 class file_index
  7 {
  8   public function set_file_index($file_name, $cur_count, $total_count)
  9   {
 10     $this->file_name = $file_name;
 11     $this->cur_count = $cur_count;
 12     $this->total_count = $total_count;
 13   }
 14   public function get_file_name()
 15   {
 16     return $this->file_name;
 17   }
 18   public function get_cur_count()
 19   {
 20     return $this->cur_count;
 21   }
 22   public function get_total_count()
 23   {
 24     return $this->total_count;
 25   }
 26 
 27   private $file_name;  //文件名
 28   private $cur_count;  //当前记录数
 29   private $total_count; //总记录数
 30 }
 31 
 32 function create_file_index(array $params)
 33 {
 34   $index = new file_index();
 35   $index->set_file_index($params['file_name'], 
 36       $params['cur_count'], $params['total_count']);
 37   return $index;
 38 }
 39 
 40 function create_file_node(DOMDocument $doc, file_index $index)
 41 {
 42   //创建一个file元素
 43   $file = $doc->createElement("file");
 44   //创建一个属性元素
 45   $name_attr = $doc->createAttribute("name");
 46   //将该属性添加到file元素上
 47   $file->appendChild($name_attr);
 48 
 49   //创建一个文本元素
 50   $file_name = $doc->createTextNode($index->get_file_name());
 51   //将文本元素添加name_attr属性上
 52   $name_attr->appendChild($file_name);
 53 
 54   //创建一个cur_count元素
 55   $cur_count = $doc->createElement("cur_count", strval($index->get_cur_count()));
 56   //将cur_count添加到file元素下
 57   $cur_count = $file->appendChild($cur_count);
 58 
 59   //创建一个total_count元素
 60   $total_count = $doc->createElement("total_count",
 61       strval($index->get_total_count()));
 62   //将total_count添加到file元素下
 63   $total_count = $file->appendChild($total_count);
 64 
 65   return $file;
 66 }
 67 
 68 function create_index_file($index_file_name, array $params)
 69 {
 70   //创建一个文档
 71   $doc = new DOMDocument("1.0", "utf-8");
 72   //创建根元素
 73   $root = $doc->createElement("index");
 74   $root = $doc->appendChild($root);
 75 
 76   //创建一个索引结构
 77   $index = create_file_index($params);
 78   $file = create_file_node($doc, $index);
 79 
 80   //将file看到根元素下 
 81   $root->appendChild($file);
 82   $doc->save($index_file_name);
 83   return true;    
 84 }
 85 
 86 function add_index_file($index_file_name, array $params)
 87 {
 88   //创建一个文档
 89   $doc = new DOMDocument();
 90   //加载xml文件
 91   $doc->load($index_file_name);
 92   //获取index元素列表
 93   $node_list = $doc->getElementsByTagName('index');
 94   //获取根元素
 95   $root = $node_list->item(0);
 96   //创建一个索引结构
 97   $index = create_file_index($params);
 98   $file = create_file_node($doc, $index);
 99   //将file看到根元素下 
100   $root->appendChild($file);
101   $doc->save($index_file_name);
102 }
103 
104 function traverse_file_index($index_file_name)
105 {
106   $file_index = array();
107   $doc = new DOMDocument();
108   $doc->load($index_file_name);
109   //获取file元素集合
110   $file_list = $doc->getElementsByTagName('file');
111   //获取cur_count元素集合
112   $cur_count_list = $doc->getElementsByTagName('cur_count');
113   //获取total_count元素集合
114   $total_count_list = $doc->getElementsByTagName('total_count');
115   for ($i = 0; $i < $file_list->length; $i++) {
116     $index = new file_index();
117     //获取file元素的name属性值
118     $file_name = $file_list->item($i)->attributes->getNamedItem("name")->nodeValue;
119     $index->set_file_index($file_name, $cur_count_list->item($i)->nodeValue,
120         $total_count_list->item($i)->nodeValue);
121     $file_index[$i] = $index;
122   }
123 
124   return $file_index;
125 }
126 
127 /*****************for test*********************/
128 $params = array();
129 $index_file_name = INDEX_FILE_NAME;
130 
131 if (file_exists($index_file_name)) {
132   $params['file_name'] = "student_info_2014_02_12";
133   $params['cur_count'] = 10;
134   $params['total_count'] = 10;
135   echo "Add index to file.\n";
136   add_index_file($index_file_name, $params);
137 }
138 else {
139   $params['file_name'] = "student_info_2014_02_11";
140   $params['cur_count'] = 23;
141   $params['total_count'] = 33;
142   echo "Create index file.\n";
143   create_index_file($index_file_name, $params);
144 }
145 
146 //测试读取xm文件
147 echo "Read index content from ".$index_file_name."\n";
148 echo "file_name\t\t\tcur_count\ttotal_count.\n";
149 $file_index = traverse_file_index($index_file_name);
150 foreach($file_index as $index) {
151   echo $index->get_file_name();
152   echo "\t\t  ";
153   echo $index->get_cur_count();
154   echo strval($cur_count);
155   echo "\t\t  ";
156   echo $index->get_total_count();
157   echo "\n";
158 }
复制代码

测试结果如下图所示:

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章