您好,欢迎访问一九零五行业门户网

详细介绍XML原理代码实例

xml 简介 
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>
实体引用:就是特殊字符的转意。
&lt; < 小于
&gt; > 大于
&amp; & 和号
&apos; ' 单引号
&quot; " 引号
<message>if salary &lt; 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原理代码实例的详细内容。
其它类似信息

推荐信息