PHPl使用DOM XML操作XML[總結]

來源:互聯網
上載者:User
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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.