Phpxml getting started _ PHP Tutorial

Source: Internet
Author: User
Phpxml entry-level learning materials. Cause: When I was working on a project today, I encountered a problem: I needed to dynamically update the pictures on the home page to show that this site was not completed, and someone was maintaining it all the time. Well, what's the cause of the demand:
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:

The code is as follows:




1
Pic 1


2
Pic 2


3
Pic 3



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 memory prototype is:
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: 1
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.

The supervisor encountered a problem when working on the project today: it was necessary to dynamically update the pictures on the home page to show that the website was not completed, and someone was maintaining it all the time. Okay. how can I solve the problem...

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.