温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

XML原理的示例分析

发布时间:2021-09-17 13:50:37 来源:亿速云 阅读:79 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关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原理的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xml
AI