PHP XML3 operation: Create an XML document using a DOM object
Source: Internet
Author: User
But the author himself in writing code is also encountered a problem, the file format can only be UTF-8, but when creating a DOM object, there is a parameter that can select encoding. If you know what is going on, contact me. In "XML entry 6: loading XML documents with DOM objects", we have learned how to use the php dom extension component to load XML documents and print them on the screen. Let's first take a look at the methods of two php dom classes:
1. createElement (name [, value])It creates an element node named name and adds a text node to the element. Value is an optional parameter that represents the value of a text element.
2. appendChild (name)It adds an element node named name to a specified element node.
Today, we use the php dom extension component to create an XML document.
Let's take a look at the results we need: Jiang Shen 23 Male 5.9 9: 00
The php code is as follows: FormatOutput = true; // ================================================ ========================================================== ========/// create the root element company $ company =$ doc-> createElement ('Company '); // add to the document $ doc-> appendChild ($ company ); // ================================================ ========================================================== ========/// create the tag element people $ people = $ doc-> createElement ('others '); // Create an id = 1 attribute for the people tag $ people-> setAttribute ("id", "1"); // add the people tag to c $ Company-> appendChild ($ people ); // ================================================ ========================================================== ========/// create a tag element name $ name = $ doc-> createElement ("name ", "Jiang Shen"); // add the name tag to the people Element $ people-> appendChild ($ name ); // ================================================ ========================================================== ========/// create a tag element age $ age = $ doc-> createElement ("age ", "23"); // add the age tag to the people Element $ people-> appendChild ($ Age ); // ================================================ ========================================================== ========/// create tag element sex $ sex = $ doc-> createElement ("sex ", "Male"); // add the sex tag to the people Element $ people-> appendChild ($ sex ); // ================================================ ========================================================== ========/// create a tag element date $ date = $ doc-> createElement ("date "); // add the date label to the people Element $ people-> appendChild ($ date ); // ==================================== ========================================================== ==================/// Create a tag element day $ day = $ doc-> createElement ("day ", "5.9"); // add the day tag to the date element $ date-> appendChild ($ day ); // ================================================ ========================================================== ========/// create a tag element hour $ hour = $ doc-> createElement ("hour ", "9:00"); // add the hour tag to the date element $ date-> appendChild ($ hour ); // ================================================ ==================================== ======================/// Save the dom document as a string and output the echo $ doc-> saveXML ($ doc) on the screen ); // ================================================ ========================================================== ========/// write file $ file_resource = fopen ('. /2. xml ', 'w'); fwrite ($ file_resource, $ doc-> saveXML ($ doc); fclose ($ file_resource);?> But the author himself in writing code is also encountered a problem, the file format can only be UTF-8, but when creating a DOM object, there is a parameter that can select encoding. If you know what is going on, contact me.
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.