Document directory
In the previous section, we talked about the basic syntax and elements, entity and DTD constraints of XML files. Next we will officially enter the XML programming of PHP to operate XML files using PHP Technology.
There are three common technologies:
1. php dom
2. php uses XPath to operate XML
3. simplexml Technology
First, describe dom
The core idea of dom (Document Object Model) Document-object-model is to regard an XML file as an object model and then operate XML files through objects.
1. When creating an XML file, you can find that the XML file directly compiled by you cannot be correctly identified when calling the XML function, therefore, create an XML file using PHP and then perform operations to view the Code directly.
<? PHP // instantiate a domdocument object $ dom = new domdocument ('1. 0', 'utf-8'); // create an IF (! File_exists ("xml_test.xml") {header ("Content-Type: text/plain"); $ root = $ dom-> createelement ("class "); $ dom-> appendchild ($ root); $ dom-> Save ("xml_test.xml");} else {$ dom-> load ("xml_test.xml ");} print $ dom-> savexml ();?>
Open on the webpage and execute the command to create an xml_test.xml file (the problem here is that the root directory name cannot use Chinese characters, but I don't know how to set it? Thank you very much for your advice !)
<?xml version="1.0" encoding="UTF-8"?><class/>
2. Add Elements
<? PHP // addelementsxml. PHP $ dom = new domdocument ("1.0", "UTF-8"); $ dom-> load ("xml_test.xml "); $ root_class = $ dom-> getelementsbytagname ("class"); $ I = 0; // For ($ I = 0; $ I <4; $ I ++) {$ root_class_node = $ root_class-> item ($ I); $ stu_node = $ dom-> createelement ("student"); $ stu_node-> setattribute ("xingbie ", "Man"); $ stu_node_name = $ dom-> createelement ("name", "name ". $ I); // set the attribute $ stu_node_name-> setattribute ("He L "," 23 "); $ stu_node_age = $ dom-> createelement (" Age "," 21 "); $ stu_node_introduce = $ dom-> createelement (" introduce ", "1111"); $ stu_node-> appendchild ($ stu_node_name); $ stu_node-> appendchild ($ stu_node_age); $ stu_node-> appendchild ($ stu_node_introduce ); $ root_class_node-> appendchild ($ stu_node); //} $ dom-> Save ("xml_test.xml"); print $ dom-> savexml ();?>
3. Traverse Elements
<? PHP // getnode. PHP // parse a file step // 1 create an object that represents the document $ dom = new domdocument ("1.0", "UTF-8"); // 2 formulated to load that XML, parse the file $ dom-> load ("xml_test.xml"); // 3 get your new node $ stu_nodes = $ dom-> getelementsbytagname ("student "); for ($ I = 0; $ I <$ stu_nodes-> length; $ I ++) {// retrieve each student $ stu_node = $ stu_nodes-> item ($ I ); for ($ I = 0; $ I <$ stu_node-> childnodes-> length; $ I ++) {echo $ stu_node-> childnodes-> item ($ I) -> nodevalue; echo "<br/>" ;}}?>
4. Delete Elements
<? PHP // parse a file step // 1 create an object that represents the document $ dom = new domdocument ("1.0", "UTF-8"); // 2 formulated to load that XML, parse the file $ dom-> load ("xml_test.xml"); // 3 get your new node $ stu_nodes = $ dom-> getelementsbytagname ("student "); $ stu_node = $ stu_nodes-> item ($ stu_nodes-> length-1); $ stu_node-> parentnode-> removechild ($ stu_node ); $ dom-> Save ("xml_test.xml");?>
5. Modify element attributes
<? PHP // 1 create an object that represents the document $ dom = new domdocument ("1.0", "UTF-8"); // 2 formulated to load that XML, parse the file $ dom-> load ("xml_test.xml"); // find the student $ stus = $ dom-> getelementsbytagname ("Age")-> item (0 ); $ stus-> nodevalue = 100; $ dom-> Save ("xml_test.xml");?>
The above introduces the basic Dom operation, add, delete, modify, query, the next section will have a specific instance to operate reference: http://www.php.net/manual/en/class.domdocument.php