PHP Pl operating XML with DOMXML [summary]

Source: Internet
Author: User
1. preface the XML tree structure is clear and suitable for configuration files. In PHP, DOMXML can be used to operate XML. This article summarizes how PHP creates, adds, and queries XML files using DOMXML. 2. the nodes in DOMXMLXML are divided into elements and text, DOMDocumen... "/> <scripttype =" text/javascript 1. the structure of the forward XML tree is clear and is very suitable for configuration files. In PHP, dom xml can be used to operate XML. This article summarizes how PHP creates, adds, and queries XML files using dom xml. 2. nodes in dom xml are divided into elements and texts. the DOMDocument type is document type and provides the member functions and attributes of Operation elements and texts. DOMDocument class: Copy the code 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 delimiter (string $ name) specify partition (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: appendChild (DOMNode $ newnode) DOMNode: cloneNode ([bool $ deep]) bool DOMNode:: hasAttributes (void) bool DOMNode: hasChildNodes (void) DOMNode: empty (DOMNode $ newnode [, DOMNode $ refnode]) bool DOMNode: empty (string $ namespaceURI) bool DOMNode: isSameNode (DOMNode $ node) bool DOMNode: isSupported (string $ feature, string $ version) string DOMNode: lookupNamespaceURI (string $ prefix) string DOMNode :: callback (string $ response) void DOMNode: normalize (void) DOMNode: removeChild (DOMNode $ oldnode) DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode )} the replication code element node class DOMElement type is defined as follows: Copy code 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 detail (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 events (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 trim (DOMAttr $ attr, bool $ isId) 24 void setIdAttributeNS (string $ namespaceURI, string $ localName, bool $ isId) 25/* Inherited methods */26 DOMNode :: appendChild (DOMNode $ newnode) 27 DOMNode: cloneNode ([bool $ deep]) 28 bool DOMNode: hasAttributes (void) 29 bool DOMNode: hasChildNodes (void) 30 DOMNode: Invoke (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: removeChild (DOMNode $ oldnode) 38 DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode) 39} The Copy code text type DOMText is defined as follows: Copy the code 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)} The Copy code DOMNode node is defined as follows: copy the code 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 $ previussibling; public readonly DOMNode $ nextSibling; public readonly slave $ 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 nodes (DOMNode $ newnode [, DOMNode $ refnode]) bool blocks (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)} copy Code 3. test program Copy code 1 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; // file name 28 private $ cur_count; // The number of current records 29 private $ total_count; // total number of records 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 // create a file element 43 $ file = $ doc-> createElement ("file "); 44 // Create an attribute element 45 $ name_attr = $ doc-> createAttribute ("name "); 46 // add this attribute to the file Element 47 $ file-> appendChild ($ name_attr ); 48 49 // create a text element 50 $ file_name = $ doc-> createTextNode ($ index-> get_file_name ()); 51 // add the name_attr attribute to the text element 52 $ name_attr-> appendChild ($ file_name ); 53 54 // Create a cur_count element 55 $ cur_count = $ doc-> createElement ("cur_count", strval ($ index-> get_cur_count ())); 56 // add cur_count to 57 $ cur_count = $ file-> appendChild ($ cur_count) under the file element ); 58 59 // Create a total_count element 60 $ total_count = $ doc-> createElement ("total_count", 61 strval ($ index-> get_total_count ())); 62 // add total_count to 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 // create a document 71 $ doc = new DOMDocument ("1.0", "UTF-8 "); 72 // Create a root element 73 $ root = $ doc-> createElement ("index"); 74 $ root = $ doc-> appendChild ($ root ); 75 76 // create an index structure 77 $ index = create_file_index ($ params); 78 $ file = create_file_node ($ doc, $ index ); 79 80 // You can view 81 $ root-> appendChild ($ file) under the root element; 82 $ doc-> save ($ index_file_name); 83 return true; 84} 85 86 function add_index_file ($ index_file_name, array $ params) 87 {88 // create a document 89 $ doc = new DOMDocument (); 90 // load the xml file 91 $ doc-> load ($ index_file_name); 92 // Obtain the index element list 93 $ node_list = $ doc-> getElementsByTagName ('index '); 94 // get the root element 95 $ root = $ node_list-> item (0); 96 // create an index structure 97 $ index = create_file_index ($ params ); 98 $ file = create_file_node ($ doc, $ index); 99 // you can see 100 $ root-> appendChild ($ file) under the root element ); 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 // Obtain the file element set 110 $ file_list = $ doc-> getElementsByTagName ('file '); 111 // Obtain the cur_count Element Set 112 $ cur_count_list = $ doc-> getElementsByTagName ('cur _ count '); 113 // Obtain the total_count element set 114 $ total_count_list = $ doc-> getElementsByTagName ('total _ count'); 115 for ($ I = 0; $ I <$ file_list-> length; $ I ++) {116 $ index = new file_index (); 117 // Obtain the name attribute value of the file element 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 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 // test reading xm file 147 echo" Read index content from ". $ index_file_name. "\ n"; 148 echo "file_name \ 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"; 153 echo $ index-> get_cur_count (); 154 echo strval ($ cur_count); 155 echo "\ t "; 156 echo $ index-> get_total_count (); 157 echo "\ n"; 158}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.