Example of how PHP creates an xml document based on DOM and domxml document
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:
<Booklist> <book id = "1"> <title> tianlong Babu </title> <author> Jin Yong </author> <content> <! [CDATA [tianlong Babu is a martial arts novel written by Jin Yong. It looks great! ]> </Content> </book> </booklist>
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:
<? Phpheader ('content-Type: text/xml; '); $ dom = new DOMDocument ('1. 0', 'utf-8'); // create a DOM object $ no1 = $ dom-> createElement ('booklist'); // create a common node: booklist $ dom-> appendChild ($ no1); // Add the booklist node to the DOM document $ no2 = $ dom-> createElement ('book '); // create the 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 Babu $ no3-> appendChild ($ no4); // Add a 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 document written by Jin Yong Novel, very nice! '); // 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 (); */?>
PS: Here are some online tools for xml operations for your reference:
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat
XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress
XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat