PHP XML Introductory learning materials _php Tips

Source: Internet
Author: User
Cause:
Today, the project encountered a problem: the need to dynamically update the home page pictures, to show that this site is not finished even if it is someone has been in maintenance. Well, the demand has, how to achieve?!
My idea is as follows:
Picture storage location: put in a folder; Type of picture: cannot be fixed, as long as the picture can upload display; Picture name: According to the original name a bit irregular, should be renamed.
Position displayed: This requires a corresponding ID to be assigned to the picture, which is fixed and corresponds to the picture one by one. So there is a record of one by one of the corresponding relationship file, you can select a CSV file, select the database records, the final decision to choose the XML, this thing in the school has not learned, I have been avoiding the use of this thing, afraid of trouble. Today is a challenge, spent the afternoon, finally some harvest.
Learning steps:
Clear goal: 1, understand the structure of XML; 2, how to build XML files dynamically, 3, how to read and modify XML files
first, the structure of XML is a tree-shaped structure:
This is a good understanding. Write a simple:
Copy Code code 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>

second, I use PHP to create:
1. Define a DOM object: $dom = new DOMDocument (' 1.0 ');
2. Add child elements: $dom->appendchild ($dom->createelement ("Pictures"))
The prototype in memory is:<pictures></pictures>
Continue to add the child element: *->appendchild ($dom->createelement ("picture"));
Continue to add: **->appendchild ($dom->createelement ("id"));
No child elements, plus node: ***->appendchild ($dom->createnode ("1"))
The above * represents the code on the previous line, so that it can be written in one line:
$dom->appendchild ($dom->createelement ("Pictures"))->appendchild ($dom->createelement ("Picture")
->appendchild ($dom->createelement ("id"))->appendchild ($dom->createnode ("1"));
Now this should be the:<pictures><picture><id>1</id></picture></pictures> in memory
Obviously the request is still far, it is easy to see Meng.
Thus generally this is written: $pictures = $dom->appendchild ($dom->createelement ("Pictures"));
$picture = $pictures->appendchild ($dom->createelement ("picture"));
$id = $picture->appendchild ($dom->createelement ("id"));
$id->appendchild ($dom->createnode ("1"));
The following can also be followed by the creation of the name node:
$name = $picture->appendchild ($dom->createelement ("name"));
$name->appendchild ($dom->createnode ("Pic 1"));
The next step is to create the picture node:
$picture = $pictures->appendchild ($dom->createelement ("picture"));
In fact, these troublesome things can be written in a for loop to achieve.
To generate an XML file:
$dom->formatoutput = true;//Set formatted output
$dom->save ("Erhsh.xml");//Save XML file
read the XML file.
1, or define a DOM object; $dom->new DOMDocument ();
2, loading XML file: $dom->load ("Erhsh.xml");
3, according to the name of the node to obtain a set of nodes: $dom->getelementbytagname ("pictures");
This method is a bit cumbersome, reference file:
Http://www.jb51.net/article/25853.htm

But there is one way I like it: simplexml_load_file ("Erhsh.xml");
This method can convert the contents of an XML file into an object form, using the "->" Knot and "[]" The content of the XML that is easy to go;
But there are a few problems with development:
When executing: print_r ($xml->pictures), the output is a SimpleXMLElement object, ([picture] => Array ([0]=>array (...) [1]=>array (...))];
Re-execution: Print_r ($xml->pictures->picture); output is n separate objects.
Execution: 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 objects",
echo output should be used, Print_r (), var_dump () output is inaccurate. Reference Address: http://www.jb51.net/article/25852.htm
Of course, you can also modify the value of XML through this method.
The writing is very bad, only for my own memo.

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.