This article mainly introduces how to create an xml document based on DOM in PHP, and analyzes the related operation skills of using DOM to create an xml file in php in the form of examples, for more information about how to create an xml document in PHP based on DOM, see the next article. This article analyzes the related operation skills of using DOM to create xml files in php based on examples, for more information, see
This example describes how to create an xml document based on DOM in PHP. We will share this with you for your reference. The details are as follows:
DOM creates xml documents
Use dom to create the following documents:
Tianlong BabuJin Yong
天龙八部是金庸写的一本武侠小说,非常好看!
Steps:
1. create a DOM object. 2. create a node. 3. create a lower-level node. 4. add a lower-level node to a higher-level node. 5. create an attribute node. 6. add an attribute node to a node that owns this attribute ~ Step 6 -- "8. add the highest level node (namely the root node) to the DOM object. --" 9. open or store the xml document.
You can create a node from either the lower-level node or the root node. The implementation code is as follows:
CreateElement ('booklist'); // create a common node: booklist $ dom-> appendChild ($ no1 ); // add the booklist node to the DOM document $ no2 = $ dom-> createElement ('book'); // Create a book node $ no1-> appendChild ($ no2 ); // add the book node to the booklist node $ no3 = $ dom-> createAttribute ('id'); // Create an attribute node: id $ no3-> value = 1; // assign $ no2-> appendChild ($ no3) to the attribute node; // add the attribute node to the book node $ no3 = $ dom-> createElement ('title '); $ no2-> appendChild ($ no3); $ no4 = $ dom-> createTextNode ('tianlong Babu '); // create a text node: Tianlong 8 Department $ no3-> appendChild ($ no4); // add the Tianlong Babu node to the book node $ no3 = $ dom-> createElement ('author '); $ no2-> appendChild ($ no3); $ no4 = $ dom-> createTextNode ('jinyong '); // create a text node: tianlong Babu $ no3-> appendChild ($ no4); // add the Tianlong Babu node to the book node $ no3 = $ dom-> createElement ('content '); $ no2-> appendChild ($ no3); $ no4 = $ dom-> createCDATASection ('tianlong Babu is a martial arts novel written by Jin Yong. it looks great! '); // Create the file CDATA node $ no3-> appendChild ($ no4); // add the Tianlong Babu node to the header ('content-type: text/html; charset = utf-8 '); echo $ dom-> save ('booklist. XML ')? 'Storage successfully': 'Storage failed'; // The storage is an xml document/* open the header directly in xml document format ('content-type: text/XML '); echo $ dom-> savexml (); */?>
For more articles about how to create xml documents based on the DOM in PHP, refer to the PHP Chinese website!