Clear goals: 1. Understand the xml structure; 2. How to dynamically create xml files; 3. How to read and modify xml files
1. The xml structure is a tree structure:
This is easy to understand. Write a simple example:
1 <pictures> 2 <picture> 3 <id> 1 </id> 4 <name> pic 1 </name> 5 </picture> 6 <picture> 7 <id> 2 </id> 8 <name> pic 2 </name> 9 </picture> 10 <picture> 11 <id> 3 </id> 12 <name> pic 3 </name> 13 </picture> 14 </pictures>
2. Create a php Tutorial:
1. Define a dom object: $ dom = new domdocument ('1. 0 ');
2. Add sub-elements: $ dom-> appendchild ($ dom-> createelement ("pictures "))
The prototype in the memory is: <pictures> </pictures>
Add child elements to the table: *-> appendchild ($ dom-> createelement ("picture "));
Continue to add: **-> appendchild ($ dom-> createelement ("id "));
If no child element is added, add a node: ***-> appendchild ($ dom-> createnode ("1 "))
The above * indicates the code of the previous line. In this way, you can write a line:
$ Dom-> appendchild ($ dom-> createelement ("pictures")-> appendchild ($ dom-> createelement ("picture "))
-> Appendchild ($ dom-> createelement ("id")-> appendchild ($ dom-> createnode ("1 "));
The current memory should be like this: <pictures> <picture> <id> 1 </id> </picture> </pictures>
Obviously, the requirement is far away, and it is easy to look at tracing.
Therefore, write $ pictures = $ dom-> appendchild ($ dom-> createelement ("pictures "));
$ Picture = $ pictures-> appendchild ($ dom-> createelement ("picture "));
$ Id = $ picture-> appendchild ($ dom-> createelement ("id "));
$ Id-> appendchild ($ dom-> createnode ("1 "));
You can create a name node as follows:
$ Name = $ picture-> appendchild ($ dom-> createelement ("name "));
$ Name-> appendchild ($ dom-> createnode ("pic 1 "));
Next, create a picture node:
$ Picture = $ pictures-> appendchild ($ dom-> createelement ("picture "));
In fact, you can write a for loop to implement these troublesome tasks.
Generate an xml file:
$ Dom-> formatoutput = true; // sets the formatting output.
$ Dom-> save ("erhsh. xml"); // save the xml file
3. Read xml files.
1. Define a dom object; $ dom-> new domdocument ();
2. load the xml file: $ dom-> load ("erhsh. xml ");
3. Get the node set by node name: $ dom-> getelementbytagname ("pictures ");
This method is a bit troublesome, refer to the File: http://wenku.baidu.com/view/8f0c3c5177232f60ddcca163.html
But there is one method I like: simplexml_load_file ("erhsh. xml ");
This method can convert the content of an xml file into an object, using "->" knot and "[]" to easily remove the xml content;
However, I still encountered a problem during development:
When print_r ($ xml-> pictures) is executed, the output is a simplexmlelement object ([picture] => array ([0] => array (...) [1] => array (...)));
Run print_r ($ xml-> pictures-> picture). n separate objects are output.
Run print_r ($ xml-> pictures-> picture [0]-> id). The output is still an object. This is not understandable. It should be a string. Finally, the Internet says "iterative object ",
Echo output should be used. print_r (), var_dump () output is not accurate.