XML 被设想用来传输和存储数据。相似于JSON。
XML 指可扩大标记言语(EXtensible Markup Language)
XML 是一种标记言语,很相似 HTML
XML 的设想主旨是传输数据,而非显现数据
XML 标签没有被预定义。您须要自行定义标签。
XML 被设想为具有自我形貌性。
XML 是 W3C 的引荐规范
XML 被设想用来构造化、存储以及传输信息。(没有花样两边很难晓得信息的构造内容)
实例:
<?xml version="1.0" encoding="utf-8"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
实例诠释:
第一行是 XML 声明。它定义 XML 的版本 (1.0) 和所运用的编码 (ISO-8859-1 = Latin-1/西欧字符集)。
形貌文档的根元素
<note>//根元素的最先 ** </note> //根元素的末端
4个子元素
<to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body>
就这样XML文档会构成一种树构造,以下:
<?xml version="1.0" encoding="utf-8"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body> <A>George</A> <B>John</B> <C>Reminder</C> </body> </note>
一切 XML 元素都须有封闭标签
<p>This is a paragraph //错 <p>This is a paragraph</p> //对
XML 标签对大小写敏感
<Message>这是毛病的。</message> //错 <message>这是准确的。</message> //对
XML 必需准确地嵌套
<b><i>This text is bold and italic</b></i> //错 <b><i>This text is bold and italic</i></b> //对
XML 的属性值须加引号
<note date=08/08/2008> //错 <to>George</to> <from>John</from> </note> <note date="08/08/2008"> //对 <to>George</to> <from>John</from> </note>
实体援用:就是特别字符的转意。
< < 小于
> > 大于
& & 和号
' ' 单引号
" " 引号
<message>if salary < 1000 then</message>
想要的:
<message>if salary < 1000 then</message>
XML 中的解释
<!-- This is a comment -->
在 XML 中,空格会被保存
HTML 会把多个一连的空格字符裁减(兼并)为一个:
HTML:Hello my name is David. 输出:Hello my name is David.
在 XML 中,文档中的空格不会被删省。
XML 元素
最先标签直到(且包括)完毕标签的部份。如:
<from>John</from>
元素可包括其他元素、文本或许二者的混合物。元素也能够具有属性。如:
<book category="CHILDREN"> //category(属性) <title>Harry Potter</title> //book的子元素,这个子元素只要文本内容 <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
XML 定名划定礼貌
XML 元素必需遵照以下定名划定礼貌:
称号能够含字母、数字以及其他的字符
称号不能以数字或许标点符号最先
称号不能以字符 “xml”(或许 XML、Xml)最先
称号不能包括空格
XML 元素 vs. 属性
<person sex="female"> //属性 <firstname>Anna</firstname> <lastname>Smith</lastname> </person> <person> <sex>female</sex> //元素 <firstname>Anna</firstname> <lastname>Smith</lastname> </person>
没有什么礼貌能够通知我们什么时候该运用属性,而什么时候该运用子元素,在 XML 中,
您应当只管防止运用属性。假如信息觉得起来很像数据,那末请运用子元素吧。
属性没法包括多重的值(元素能够)
属性没法形貌树构造(元素能够)
属性不容易扩大(为将来的变化)
属性难以浏览和保护
XML 定名空间(XML Namespaces)
XML 定名空间供应防止元素定名争执的要领。
这个 XML 文档照顾着某个表格中的信息:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
这个 XML 文档照顾有关桌子的信息(一件家具):
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
因为两个文档都包括带有差别内容和定义的 <table> 元素,就会发作定名争执。
运用前缀来防止定名争执
<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table>
运用定名空间(Namespaces)
<h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>
此 XML 文档照顾着有关一件家具的信息:
<f:table xmlns:f="http://www.w3school.com.cn/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table>
默许的定名空间(Default Namespaces)
为元素定义默许的定名空间能够让我们省去在一切的子元素中运用前缀的事情。
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
此 XML 文档照顾着有关一件家具的信息:
<table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
定名空间是就近准绳的
<?xml version="1.0" encoding="utf-8"?> <root xmlns="dotnet" xmlns:w="wpf"> <!-- xmlns: dotnet --> <a>data in a</a> //默许的定名空间 <!-- xmlns: dotnet --> <w:b>data in b</w:b> //w定名空间 <!-- xmlns: wpf --> <c xmlns="silverlight"> <!-- xmlns: silverlight --> <w:d> <!-- xmlns: wpf --> <e>data in e</e> <!-- xmlns: silverlight --> //就近准绳 </w:d> </c> </root>
CDATA
术语 CDATA 指的是不应由 XML 剖析器举行剖析的文本数据(Unparsed Character Data)。(就是内里的数据不举行XML剖析)
CDATA 部份由 "<![CDATA[" 最先,由 "]]>" 完毕:
<script> <![CDATA[ //最先 function matchwo(a,b) { if (a < b && a < 0) then { return 1; } else { return 0; } } ]]> //完毕 </script>
以上就是细致引见XML道理代码实例的细致内容,更多请关注ki4网别的相干文章!