Basic xml learning notes 01
Continue to learn the third step of the classical trilogy: xml, which is just a learning note. It basically records the teacher's explanation and integrates some of his own understandings. It is not original. For original content, see the video of 18th brother. I silently sent 32 likes to 18 elder brothers. I just learned the smarty template engine a few days ago. Because I have used it in the company, some smarty will be used, but it is still not clear, I looked at it. No notes, template engine! You only need to use it. There is no need to study too many advanced things. After all, it is more important to master them now. As for the rich experience and time, you can take a good look at the source code. Let's talk about xml. I have read it briefly before. http://www.w3school.com.cn/ Introduction to xml. I just browsed one concept. I still don't feel anything about how to use it! Most of the time, you still need to watch the teacher or others do it to know when to use it, where to use it, and how to use it. You are a little stupid! Ah... Copy a section about xml in w3school first! 1. What is XML? XML refers to the EXtensible Markup Language. XML is a Markup Language. Similar to html xml, it is designed to transmit data, rather than display data. XML labels are not predefined. You need to customize the tag. XML is designed to be self-descriptive. XML is W3C recommendation Standard 2. The main difference between XML and HTML is not an alternative to HTML. XML and HTML are designed for different purposes: XML is designed for data transmission and storage, with the focus on data content. HTML is designed to display data. Its focus is on the appearance of the data. HTML is designed to display information, while XML is designed to transmit information. 3. Learning notes XML syntax specification 1. How to declare an xml file? The declaration part includes the xml version and encoding (version 1.0 only). <? Xml version = '1. 0' encoding = 'utf-8'?> <Node name> </node name> note: in xml, nodes, elements, and labels are all meanings. 2. tags must be written in pairs, for example, the <name> </name> label is case sensitive. in xml, all tags are in lowercase or used as single tags, self-closing and <name/> (when only the <root> </root> and the label have no content, the browser considers it as <root/>) 3. The entire document must have one root node and only one root node. 4. labels can be nested, but cannot be nested with each other. <root> <name> </root> </name> error 5. Attribute writing attributes must have a value. lowercase letters are recommended, attribute values must be enclosed in double quotation marks. 6. Comments <! -- Write xml comments here, the xml and html annotation writing rules are the same --> 7. special characters must be replaced by entities '-> & apos "-> & quot>-> & gt <-> & lt 8. If a large text segment exists, for example, if an article is extracted from the database, there are a lot of ',', >,<, isn't it necessary to replace one side with an entity? The CDATA node indicates that the CDATA node is used to enlarge the segment file, so you do not need to consider the entity <! [CDATA [write your content here]> 9. In xml, everything is a node, including line breaks and blank spaces. All are text nodes (xml is plain text, so these are naturally nodes.) xml DOM parses xml 1, document. are you familiar with the getElementsByTagName (); childNodes () methods? A: Yes. Why are there in js and java? Because xml is a strict document format, especially its own standards. Xml parsing also has its standard, dom standard. The HTML and xml we use all follow the Dom standard. That is why we are familiar with the above two functions, because both php, java, c, and js parse the dom tree., they all follow the same DOM standard. 2. How to parse xml through the php DOM object? Step 1: Read the xml file to form an xml document object, <-- corresponding to js --> document Object Step 2: Get a group of nodes through getElementsByTagName ('tagname') <-- js --> document. step 3 of getElementsByTagName (): Get one of the objects in step 2, the specific node is obtained. 3. The DOM parsing object $ dom = new DOMdocument ('1. 0', 'utf-8'); what is the use of DOMdocument object? It loads the xml file into the memory and analyzes it. You can use the object analysis xml to load the xml file $ dom-> load ('file address'); get the title node list, there are many title nodes, so we get the 'node list object 'ts = dom-> getElementsByTagName ('title'); print_r ($ ts); // DOMNodeList object () the DOMNodelist node list class has an attribute: length, which indicates a method for obtaining the number of nodes. item (N) indicates obtaining the nth node echo 'we get '. $ ts-> length. 'book'; DOMElement Object DOM Node Object title0 = ts-> item (0); // title0 is a Node Object DOMElementObject () title0-> childNodes print_r ($ title0-> childNod Es); The child node Object List DOMNodeList Object selects the first child Node Object, that is, the following text Object // The child node list Object here is also the same as the above mentioned, one attribute and one method text = title0-> childNodes-> item (0); print_r ($ text); // DOMText Object () text Object echo $ text-> wholeText; the following is the copy code of my demo xml file: <? Xml version = '1. 0' encoding = 'utf-8'?> 2 <bookstore> 3 <book> 4 <title> Lu Yao-ordinary world </title> 5 <price> 43 </price> 6 </book> 7 <book> 8 <title> CAO Xueqin-Dream of Red Mansions </title> 9 <price> 93 </price> 10 </book> 11 </bookstore> copy code PHP file copy code 1 <? Php 2 header ("content-type: text/html; charset = UTF-8"); 3 // create a DOM resolution object 4 $ dom = new DOMDocument ('1. 0 ', 'utf-8'); 5 print_r ($ dom); // DOMDocument Object () DOM Document Object 6 // load xml document 7 $ dom-> load ('. /02. xml '); 8 echo "<br/>"; 9 print_r ($ dom); // DOMDocument Object () 10 // select a node to obtain the node list Object DOMNodeList Object () 11 $ ts = $ dom-> getElementsByTagName ('title'); 12 echo "<br/>"; 13 print_r ($ ts); // DOMNodeList Object () 14/********************************** 15 * DOMNodeList Object () 16 * one attribute: length, representing the number of acquired nodes 17 * One method: item (N), representing obtaining the nth node, N starts from 0 and returns 18*19 * "<br/> "; 21 echo $ ts-> length; // node list length, 22 echo "<br/>"; 23 // select the first node in the list, get a Node Object 24 print_r ($ ts-> item (0); // DOMElement Object () 25 26 $ title0 = $ ts-> item (0 ); // $ title0 is a Node Object DOMElement Object () 27 echo "<br/>"; 28 print_r ($ title 0-> childNodes); // DOMNodeList Object () childNodes is an attribute, the result is that the subnode list object 29 /******************************* * *** 30 * Note that in xml, everything is a node, including text. Press enter to wrap 31 *. Therefore, the relationship between 'lu Yao-ordinary world' and title in the title is the parent-child relationship. 32 * That is why print_r ($ title0-> childNodes) is printed on the top, DOMNodeList Object (), or a node list object 33 ********************************** /34 // select the first subnode object, that is, the following text object 35 // The subnode list object here is also the same as the above mentioned, one attribute and one method 36 $ text = $ title0-> childNodes-> item (0); 37 echo "<br/>"; 38 print_r ($ text ); // DOMText Object () Text Object 39 40 echo "<br/>"; 41 // we are very close. We have obtained the Text Object, then, as long as the object content is complete, 42 // use a text object attribute wholeText43 echo $ text-> wholeText; 44 45 /****** **************************************** * 46 * now, let's take a look at the entire idea we just had 47 * Prime Minister, instantiate a Document Object: DOMDocument Object, that is, the above $ dom48 *. Second, call the road method, load the xml file $ dom-> load ('. /02. xml '); 49 * then, get a title node list object through the getElementsByTagName method: $ ts, DOMNodeList Object50 *. Then, we want to get the first title, and we use item (0) select the first Node Object DOMElement Object 51 * in the list, and then use print_r ($ title0-> childNodes) to view a node list DOMNodeList Object. Why? As described above, 52 * is not mentioned here. Finally, use $ text = $ title0-> childNodes-> item (0); select the first Node object, this Object is a DOMText Object () Text Object 53 *. Use the wholeText attribute to get information 54 * Note: The above operations can be performed consecutively, the following is an example (name of the second book is taken out ): 55 *************************************** * *****/56 echo "