Cause:
Today, I encountered a problem when I was working on the project: I needed to dynamically update the pictures on the home page to show that this site was not completed, and someone was still maintaining it. Okay. How can we achieve this if we have a requirement ?!
My ideas are as follows:
Image storage location: put in a folder; image type: cannot be fixed, as long as it is an image, you can upload and display; image name: according to the original name is a bit informal, you should rename it.
Position: You need to specify the corresponding id for the image. The id is fixed and must correspond to the image one by one. So there is a one-to-one relationship file with records. You can select a csv file, a database record, and finally select xml. This is something you have never learned at school and I have been avoiding using it, fear of trouble. Today is a challenge. It took me one afternoon to finally get some results.
Learning steps:
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:
Copy codeThe Code is as follows: <pictures>
<Picture>
<Id> 1 </id>
<Name> pic 1 </name>
</Picture>
<Picture>
<Id> 2 </id>
<Name> pic 2 </name>
</Picture>
<Picture>
<Id> 3 </id>
<Name> pic 3 </name>
</Picture>
</Pictures>
2. Create a php instance:
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://www.jb51.net/article/25853.htm
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, print_r (), and var_dump () are not accurate. Reference: http://www.jb51.net/article/25852.htm
Of course, you can also modify the xml value through this method.
The writing is poor, just for my memo.