Xml basics 02
Next, let's take the last xml note 01 and record how to create xml in DOM. Because I didn't write a blog specifically, I recorded the notes using the annotation method in the program. It is inevitable that the format is incorrect, if you are familiar with typographical components, you can read the code and add comments. You can see the methods used in the Code and the php manual. If you don't know, you can check the manual. Here is a summary of the manual: 1. DOMNode class, which is a dom root class, which defines the attributes and methods. For example: $ nodeName attribute appendChild method 2. DOMDocument class. This is a document class that inherits the domNode class and has its own attributes and Methods 3. DOMElement class, which is an element class, it also inherits the domNode class, and of course it also has its own attributes and method 4. The DOMAttr class is an attribute class, which is the same as the above 5. DOMCdataSection class, a CDATA class, same as above 6. DOMText class, for a text class, how do I create and use the corresponding class as above? The previous blog has the following objects: DOMdocument object --> DOMDocument class DOMElement Object --> DOMElement class DOMText Object --> DOMText class DOMNodeList Object: The node list object corresponds to DOMNodeList Class 2. The code is given now, use comments as blog posts. Copy code 1 <? Php 2/** 3*4 * @ authors wxb ( http://www.cnblogs.com/wxb0328/ ) 5 * @ date 22:36:02 6 * @ version $ Id $7 */8/* 9 34. DOM creates XML 10. Emphasize again: in xml, everything is a node, including line breaks and white spaces. It is a text node (xml is plain text, so these are naturally also nodes). The attribute described below is also a node, this is called attribute node 11. 2. How to generate the following xml file? 12 <? Xml version = '1. 0' encoding = 'utf-8'?> 13 <bookstore> 14 <book id = "b001"> 15 <title> Lu Yao-ordinary world </title> 16 <intro> <! [CDATA [This is a classic book & suitable for all ages]> </intro> 17 </book> 18 </bookstore> 19 how to create DOM? 20 Method 1: 21 from the center to the outside 1. Create luyao-plain world text node 22. 2. Create a common title node 23. 3. Set luyao-plain world text node, add to title node 24 4. Create CDATA node 25 5. Create intro node 26 6. Place CDATA node in intro node 27 7. Create book node 28 8. Place title and intro put the node into the book node 29 9, create id attribute node 30 10, put the attribute node in the book node 31 11, create a bookstore node 32 12, put the book node in the bookstore node 33 13. Put the bookstore node in the Document. 34. Method 2: in the above step, we extract the following operations: 37 1. How to Create a text node 38 2. How to create a common node 39 3. How to Create a CDATA node 40 4. How to Create a property node 41 5. How to add a subnode 42. Next, complete the creation of the xml file. For the specific usage and instructions of the method in the operation, check the php manual 43 */44 // create the DOM Document Object 45 $ dom = new DOMDocument ('1. 0 ', 'utf-8'); 46 47 // 1. Create a text node. 48 // call createTextNode () method 49 $ text = $ dom-> createTextNode ('luyao-ordinary World '); 50 // 2. Create the title element 51 $ title = $ dom-> createElement ('title'); 52 53 // 3. Click the text node of Lu Yao-ordinary world, add 54 $ title-> appendChild ($ text) to the title node ); 55 // 4. Create a CDATA node 56 $ cdata = $ dom-> createCDATASection ('this is a classic book & applicable to all olde'); 57 // 5. Create an intro node 58 $ intro = $ dom-> createElement ('intro '); 59 // 6. Place the CDATA node to the intro node 60 $ intro-> appendChild ($ cdata ); 61 // 7. Create a book node 62 $ book = $ dom-> createElement ('book '); 63 // 8. Place the title and intro nodes into the book node 64 $ book-> appendChild ($ title); 65 $ book-> appendChild ($ intro ); 66 // 9. Create id attribute node 67 $ id = $ dom-> createAttribute ('id'); 68 $ id-> value = 'b001 '; 69 // 10. Place the attribute node to the book node. 70 $ book-> appendChild ($ id); 71 // 11. Create a bookstore node 72 $ bookstore = $ dom-> createElement ('bookstore '); 73 // 12. Place the book node to the bookstore node 74 $ bookstore-> appendChild ($ book ); 75 // 13. Put the bookstore node in the document 76 $ dom-> appendChild ($ bookstore ); 77 78 // output to see 79/* 80*81 * Why can this method be output? I don't want to see the name. We can see 82 * string DOMDocument in the manual :: saveXML ([DOMNode $ node [, int $ options]) 83 * Returns the XML, or FALSE if an error occurred. 84 * This method can be used by default, The returned result is an xml string. Is it a string? 85 */86 // echo $ dom-> saveXML (); 87/* 88 *. We can see result 1, there is nothing on the page, but please pay attention to the tag in the header. Let's right-click the source code. 2 89 * I understand that the reason why I didn't see it on the page is that the browser parses our xml 90 *. How can I avoid browser parsing? 91 * a header message is sent to the browser, telling the browser not to parse 92 * headers ("content-type: text/xml") for xml "); 93*94 */95 // now we comment out the output that is missing, and use the following output 96 // header ("content-type: text/xml "); 97 // echo $ dom-> saveXML (); 98 // result 3 99 100/* Now we try to save the xml generated above into a file 101 * use save () method 102 * int DOMDocument: save (string $ filename [, int $ options]) 103 * filename The path to the saved XML document104 105 */106 echo $ dom-> save ('34. xml ')? "OK": "no"; 107 // The xml file is successfully generated. At the same time, 4108 109/* 110 *, we found that the generated xml file has no format, there are no spaces, no line breaks, and a line is complete. 111 * This verifies that everything in xml above is node 112 */113 114?>