Home  >  Article  >  Backend Development  >  Quick Start Tutorial for XML

Quick Start Tutorial for XML

PHPz
PHPzOriginal
2017-04-04 10:54:301781browse
<p><br></p> <h2>Chapter 1 What is XML? Quick introduction to XML</h2> <h3>1. What is XML? First, let's take a look at a piece of code </h3> <p><strong>XML</strong> (Extensible Markup Language) </p> <pre class="brush:php;toolbar:false"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE class [ <!ELEMENT class ANY>     <!ELEMENT person (name,time,msg,data)>     <!ELEMENT name (#PCDATA)>     <!ELEMENT time (#PCDATA)>     <!ELEMENT msg (#PCDATA)>     <!ELEMENT data (#PCDATA)> ]> <class>     <person>         <name>0x584A</name>         <time>2015年9月25日 10:24:41</time>         <msg>holle,world!</msg>         <!--这里是CDATA区间-->         <data><![CDATA[<XML的那些事...> -> 跟随0x584A、学习XML.. ]]></data>     </person> </class></pre> <p> Let's take a look at the specific display effect after running in the browser: </p> <p class="image-package"><img src="https://img.php.cn/upload/article/000/000/164/729e3f29ba6f560af48f387edc5ab46c-0.png" alt="Quick Start Tutorial for XML" ><br></p> <p class="image-caption">1.png</p> <h3>2.What is XML mainly used for? </h3> <p> I believe that students who have been online for a long time have seen files with the suffix <strong>.xml</strong>. This is what we call <strong>XML</strong> files. <br>First after <strong>HTML</strong> became popular, the <strong><a href="http://www.php.cn/wiki/1550.html" target="_blank">W3C</a></strong> organization felt that the limitations of the HTML language were too great, so it introduced XML for the purpose of <strong> Replace HTML language </strong>. </p> <p>Of course, it seems that W3C’s plan has not been successful. <code>(However, there are no eggs⊙﹏⊙)</code></p> <p>Although <strong>XML</strong> has not replaced <strong>HTML</strong>, its unique characteristics allow it to continue to this day. . </p> <pre class="brush:php;toolbar:false">优点: 1. 高度自定义[标签] 2. 不同语言中的数据流通规范 3. XML是用来描述数据的。而在HTML中,数据是写在HTML标签中的。 4. XML设计是用来描述数据的:重点是什么是数据,怎么存放数据。而HTML则是用来显示数据:重点是怎么显示数据,及怎么更好的显示数据。</pre> <p>Explain XML in one sentence: <code>XML is a cross-platform, software- and hardware-independent tool for processing information (a tool for data manipulation and data transmission)</code></p> <blockquote> <p>Um.. Let me share a joke with you: </p> <p>Q: When should I use XML? </p> <p>Answer: You can write in your resume that you know XML..</p> </blockquote> <h3>3. Quick introduction to XML</h3> <p>Based on the first piece of code above , let us analyze it step by step. </p> <pre class="brush:php;toolbar:false"><?xml version="1.0" encoding="UTF-8"?></pre> <p>The first one is to declare first, declare that this XML version is <strong>1.0</strong>, and use <strong>UTF-8</strong><a href="http://www.php.cn/code/225.html" target="_blank">character set</a> to identify it . </p> <p>The following code is used for <a href="http://www.php.cn/wiki/1528.html" target="_blank">DTD</a><a href="http://www.php.cn/code/12132.html" target="_blank">constraints</a>. </p> <pre class="brush:php;toolbar:false"><!-- 内部 DOCTYPE 声明 定义文档是class类型文件 --> <!DOCTYPE class [ <!-- 元素class 通过类别关键字ANY声明元素,可包含任何可解析的数据组合 -->     <!ELEMENT class ANY>     <!-- 定义person元素内有四个元素 注意:约束顺序也是节点顺序 -->     <!ELEMENT person (name,time,msg,data)>     <!-- 定义name元素 为#PCDATA类型 -->     <!ELEMENT name (#PCDATA)>     <!ELEMENT time (#PCDATA)>     <!ELEMENT msg (#PCDATA)>     <!ELEMENT data (#PCDATA)> ]></pre> <p> Then someone asked: </p> <p><strong>What is DTD? </strong></p> <p>Let’s take a look at the explanation on W3C: </p> <blockquote><p><strong>Document type definition (DTD) can define legal XML document building blocks. It uses a series of legal elements to define the structure of the document. </strong></p></blockquote> <p>Uh... <code>(Actually, it's just bullshit, it's the same as not saying it...)</code></p> <p>Okay, no more complaining. In fact, it sets restrictions on the XML elements, <a href="http://www.php.cn/wiki/169.html" target="_blank"> attributes</a>, etc. we write. The structure we write must follow DTD constraints. </p> <hr> <p>Let us continue to look at the following code: </p> <pre class="brush:php;toolbar:false"><class>     <person>         <name>0x584A</name>         <time>2015年9月25日 10:24:41</time>         <msg>holle,world!</msg>         <!--这里是CDATA区间-->         <data><![CDATA[<XML的那些事...> -> 跟随0x584A、学习XML.. ]]></data>     </person> </class></pre> <p>Did you find anything? </p> <p>Yes, yes, it is a tree structure. Let's take a look at the DTD constraint. The root node <strong>class</strong> is first defined, and then the <strong>person</strong> element is defined and there are four elements inside the <br><strong>person</strong> element. Elements <strong>name</strong>, <strong>time</strong>, <strong>msg</strong>, <strong>data</strong>. </p> <p>There is a strange thing among them, we call it <code>CDATA section</code>, only the text in the section will be ignored by the parser, so it <a href="http://www.php.cn/php/php-tp-sameoutput.html" target="_blank">outputs it as is</a>Special symbols<code><</code>、<code>></code>, etc...</p> <pre class="brush:php;toolbar:false">好了、至此关于XML的快速入门结束了,你学会了多少呢? 下一章让我们来学习XML的语法、元素及属性吧 o(∩_∩)o</pre> <p>       </p>

The above is the detailed content of Quick Start Tutorial for XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:XML parsing basicsNext article:XML parsing basics