PHP operation XML File Learning notes

Source: Internet
Author: User

Original: PHP operation XML File Learning notes

The XML file is a tag language that stores data through custom tags, and its primary role is to store data as well.

Operations for XML include traversal, build, modify, delete, and other similar operations. PHP for the operation of a lot of XML, this study is done by DOMDocument, other methods can refer to

http://www.oschina.net/code/snippet_110138_4727

1. Traversal of the XML file

By DOMDocument the operation of the XML file: First instantiate an object of the DOMDocument class, and then introduce the XML file to be manipulated. That

1 $doc New DOMDocument (); 2 $doc->load ("Test.xml");

The traversal of the XML is obtained by obtaining the tag name in the XML file to obtain the contents of the tag, for example, the contents of the XML file are:

<?XML version= "1.0" encoding= "Utf-8"?><School>    <Student>        <name>Tom</name>        <Sex>Man</Sex>        < Age>19</ Age>    </Student>    <Student>        <name>John doe</name>        <Sex>Woman</Sex>        < Age>18</ Age>    </Student>    <Student>        <name>Harry</name>        <Sex>Man</Sex>        < Age>19</ Age>    </Student></School>

Now, through PHP, each student information is traversed, and the getElementsByTagName () method gets the label of the specified name and the array, and then iterates through the information:

1 $content $doc->getelementsbytagname ("student"); 2 foreach ($contentas$value) {3     Echo $value->nodevalue. " <br/> "; 4 }

  Note: When traversing the XML file, one of the problems to be aware of is the encoding problem, if the XML file encoding is "Utf-8", then the XML file encoding must be utf-8, you can use a text editor to Save the file as the same file encoding as the XML encoding. Otherwise, when the traversal will be error or garbled.

2. Generating an XML file

When generating an XML file, the contents of each node and node are created through the methods in the DOMDocument class. I create an XML file by querying the database and generating the information in the database as follows:

1 include"Database.php";//Import Connection Database files2 $query=mysql_query("SELECT * FROM Test");3 4 //manipulating XML Preparation5 $doc=NewDOMDocument ("1.0", "Utf-8");6 7 //to create a node for an XML file by using the CreateElement method8 //Create root node9 $school=$doc->createelement ("School");Ten //to add a node or property to a specified label by using the AppendChild method One //Add the root node to the XML file A $doc->appendchild ($school); -  - //Create a student node the $student=$doc->createelement ("Student"); - //Add the Student node to the school node - $school->appendchild ($student); -  + $nameinfo=Array();//corresponds to the content under the name tag in the XML file - $sexinfo=Array();//corresponds to the contents of the sex tag in the XML file + $ageinfo=Array();//corresponds to the content under the age tag in the XML file A $idinfo=Array();//the value of the property ID in the corresponding XML file at $i= 0;//control variable i -  -  while($row=Mysql_fetch_array($query)){ -     //Assigning a value to a label node or assigning a value to a property by using the createTextNode method - //Querying database information and assigning values to the corresponding array -     $nameinfo[$i] =$doc->createtextnode ($row["Name"]); in     $sexinfo[$i] =$doc->createtextnode ($row["Sex"]); -     $ageinfo[$i] =$doc->createtextnode ($row["Age"]); to     $idinfo[$i] =$doc->createtextnode ($i); +     $i++; - } the  *  for($i= 0;$i<Count($nameinfo);$i++){ $     $name=$doc->createelement ("name");Panax Notoginseng     $sex=$doc->createelement ("Sex"); -     $age=$doc->createelement ("Age"); the     //creating attributes for node labels with createattribute +     $id=$doc->createattribute ("id"); A  the     //Add the name, sex, age tag to the student label, and assign a property ID to the student tag +     $student->appendchild ($name); -     $student->appendchild ($sex); $     $student->appendchild ($age); $     $student->appendchild ($id); -      -     //Add the value taken from the database to the corresponding label the     $name->appendchild ($nameinfo[$i]); -     $sex->appendchild ($sexinfo[$i]);Wuyi     $age->appendchild ($ageinfo[$i]); the     $id->appendchild ($idinfo[$i]); - } Wu  - //Save the generated XML file by using the Save method About $doc->save ("Test.xml");

  Note: In the generation of XML files should also pay attention to the coding problem, in addition to the XML file encoding and XML specified encoding should be consistent, in connection with the database should also pay attention to the XML specified encoding should be and the database code, in addition to the generation of XML files should pay special attention to the subordinate relationship of labels at all levels.

Modification and deletion of 3.XML files

When the data in the XML file is modified and deleted, the first thing to do is to introduce the XML of the operation in the same way as the traversal. When modifying or deleting, the property of the tag and its value are positioned by the following method:

1 $doc=NewDOMDocument ();2 $doc->load ("Test.xml");3 4 //The getElementsByTagName method is used to obtain all label contents labeled Student, and to coexist as an array $students5 $students=$doc->getelementsbytagname ("Student");6 7 //traverse the $students to save each student information as $student8 foreach($students  as $student){9     //gets the value of the specified property through GetAttributeTen //Change the name of the student label with ID 2 to KENTICNY One     if($student->getattribute ("id") ==2){ A         $content=$doc->getelementsbytagname ("name")->item (0)->nodevalue = "Kenticny"; -     } -     //Delete the student label content with ID 1 the     if($student->getattribute ("id") ==1){ -         //remove the specified label content by using the RemoveChild method - //When removing the label content, the parent tag is removed from the tag to obtain the parent label via ParentNode -         $student->parentnode->removechild ($student); +     } -  + } A //Save the modified XML file at $doc->save ("Test.xml");

  Note: When modifying and deleting XML, it is important to note that both the modification and deletion are to be positioned by the properties of the specified label, and then, at the time of deletion, should be deleted using the parent tag of the deleted tag . operation.

  

These are my Learning notes for PHP to manipulate XML files through the DOMDocument method. Welcome reprint----Reprint Please specify the source by Jin Zhi Yi April 24, 2012

PHP Action XML file Learning notes

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.