XML(一)--基本概念

来源:互联网 发布:each遍历json对象二重 编辑:程序博客网 时间:2024/05/22 00:52

关于XML的完整内容可以参考:

http://www.w3schools.com/schema/default.asp

左边可以看到一个 XML Tutorial,下面包括了XML相关的所有主题。


什么是XML

XML was designed to transport and store data.

顺便提了一句,虽然大家现在看到的HTML也是XML格式的,但HTML was designed to display data. 

同时HTML也不是必须是XML格式的。在早期的HTML中,一些tag是不需要有对应的closing tag的:

<p>This is a paragraph

但是现在的HTML基本上也都是XML格式的,但是这不是必须的。只有XHTML才是必须遵守XML标准的。


一个实际的例子及其简单说明

<?xml version="1.0" encoding="ISO-8859-1"?> \\XML declaration
<note date="12/11/2007"> \\ root element. root element是必须的.
 <to>Tove</to> \\child element
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>

1,<note>这样的标签称为一个tag,不区分level。同时XML 的tag是区分大小写的。

2,Children on the same level are called siblings (brothers or sisters).

3,tag可以有attributes。<note date="12/11/2007">中,date就是note的attribute。同时attribute的值必须用“”括起来。

4,转义:如果<to>Tove</to>这里的人名叫Gr<ace,如果直接放到XML中就会出现混乱。所以XML中定义了转义字符,对于<这类特殊字符不能直接使用,而是要改为使用:

&lt;<less than&gt;>greater than&amp;&ampersand &apos;'apostrophe&quot;"quotation mark所以上例应改为<to>Gr&lt;ace</to>

5, 在XML中使用comments,使用<!--, -->将comments括起来就行了。


详细的定义

XML element

下面这个概念非常重要:

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

不要将element和tag混为一谈。否在在使用c#,java等关于XML的类时的一些方法时会混乱。

XML attribute

Attributes provide additional information about an element. Attributes often provide information that is not a part of the data

<img src="computer.gif">

<file type="gif">computer.gif</file>

也就是说,computer.gif 属于data,而不是attribute。

There are no rules about when to use attributes or when to use elements.所以下面两种写法是完全等同的:

<img src="computer.gif"> </img> 


<img> 

  <src>computer.gif</src>

</img> 

但是attribute虽然用起来方便,但是在扩展性,层次结构方面不如element。

原创粉丝点击