Php operating xml document program code

Source: Internet
Author: User
Next, I will introduce an xml document program written by a friend, and then a function for parsing xml documents in php. if you need it, refer to it, the code is as follows :? Xmlversi... next, I will introduce an xml document program written by a friend, and then a function for parsing xml documents in php. if you need it, refer to it, the code is as follows:

/*

  
 <学生 number="101"> 
  <名字>
   
Sun Wukong
   
  <名字>
   
Sun Walker
   
  <年龄>
   
Monkey precision
   
  <介绍> 
  
 <学生 number="102"> 
  <名字>
   
Bai Gujing
   
  <年龄>
   
140
   
  <介绍>
   
Thousands of Magic
   
  
 <学生 number="103"> 
  <名字>
   
Pig
   
  <名字>
   
Incompetent pig
   
  <年龄>
   
200
   
  <介绍>
   
Sleep
   
  


*/

 Version = $ version; $ this-> encoding = $ encoding; $ this-> xml = new DOMDocument ($ version, $ encoding); if ($ xmlFile) $ this-> xml-> load ($ xmlFile);} function getRootEle ($ rootTag) {$ this-> xmlRoot = $ this-> xml-> getElementsByTagName ($ rootTag) -> item (0);} function getSeachItem ($ itemsTag, $ seachNode, $ seachValue) {$ this-> items = $ this-> xml-> getElementsByTagName ($ itemsTag ); $ this-> items-> length; for ($ I = 0; $ I <$ t His-> items-> length; $ I ++) {$ item = $ this-> items-> item ($ I ); // element $ node = $ item-> getElementsByTagName ($ seachNode); // node for ($ j = 0; $ j <$ node-> length; $ j ++) {$ subNode = $ node-> item ($ j); if ($ seachValue = $ subNode-> nodeValue) {$ this-> seachNode = $ subNode; $ this-> seachItem = $ item; $ this-> seachValue = $ subNode-> nodeValue; break (2) ;}} return ($ this-> seachNode )? True: false;} function update ($ nodeValue, $ nodeTag = '', $ append = false, $ index = 0) {if ($ append) {if ($ nodeTag) $ this-> seachItem-> getElementsByTagName ($ nodeTag)-> item ($ index)-> nodeValue + = $ nodeValue; else $ this-> seachNode-> nodeValue + = $ nodeValue;} else {if ($ nodeTag) $ this-> seachItem-> getElementsByTagName ($ nodeTag) -> item ($ index)-> nodeValue = $ nodeValue; else $ this-> seachNode-> nodeValue = $ nodeV Alue ;}} function save ($ filename) {$ this-> writeBytes = $ this-> xml-> save ($ filename); return ($ this-> writeBytes )? True: false ;}$ test = new xmlDom ('student. xml '); $ test-> getSeachItem ('student', 'age', '20140901 '); // find the pigs with age = 103 $ test-> update ('pigpigs', 'name', false, 1); // change the second name of the pigs with age =: pig $ test-> save ('new. XML'); // save it as a new file?>

The above uses dom to operate. We use SimpleXML in php to operate xml, which is also a standard class for operating xml documents.

Simplexml_load_file (string filename)

The filename variable is the file name and path used to store the XML data file. the following code creates a SimpleXML object using the simplexml_load_file function. the code is as follows:

 

Among them, the data stored in example. xml is exactly the same as the above $ data, and the running result is also the same as above.

The above two methods implement the same function. The difference is that the XML data source is different. if the XML data source is in the PHP script file, simplexml_load_string must be used for creation, if the XML data source is in a separate XML file, simplexml_load_file must be used for creation.

Read tags from XML data

Similar to operating on array variables, reading XML can also be done in a similar way. for example, if you need to read the "name" attribute under each "depart" label in the XML data above, you can use the foreach function, as shown in the following code.

 Depart as $ a) {echo "$ a-> name
";}// The running result is as follows. // Production support // testing center?>

Read the XML file, read every depart tag in the XML data cyclically, and output the name attribute. you can also use square brackets "[]" to directly read the tag specified in the XML data, the following code outputs the "name" attribute of the first "depart" tag in the preceding XML data. the code is as follows:

 Depart-> name [0]; // output node // The running result is as follows. // Production support?>

The SimpleXML component provides the children method to read all the sub-tags under a tag. for example, for the "depart" tag in the preceding XML data, there are two sub-tags: "name" and "employees", the following code reads sub-tags under the first "depart" tag.


Article link:

Save this article for favorites!

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.