当前位置: 首页 > 网络学院 > XML相关教程 > XML > xml 文档树
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
XML 文档树起始于“根元素”,并以此为基础扩展文档的分支结构。
XML documents use a self-describing and simple syntax:
XML 文档使用一种相对简单的自述性语法:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).
代码的第一行对XML文档做出了声明。它定义了XML版本号(1.0)以及文档所使用的字符编码(ISO-8859-1:拉丁文/西欧字符集)。
The next line describes the root element of the document (like saying: "this document is a note"):
接下来的一行定义了文档的根元素(指明该文档是一份便条):
<note> |
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
再接下去的4行定义了根元素的4个子元素,分别是“to”、“form”、“heading”和“body”:
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> |
And finally the last line defines the end of the root element:
文档的最后一行定义了根元素的结束标签:
</note> |
You can assume, from this example, that the XML document contains a note to Tove from Jani.
举个例子来说,你可以假设这份XML文档是Jani递交给Tove的一张便条。
Don't you agree that XML is pretty self-descriptive?
通过上述案例,对于XML是一种完美的自述性语言这点应该毋庸置疑了吧?
XML documents must contain a root element. This element is "the parent" of all other elements.
XML 文档必须包含一个根元素。这个根元素是其它所有元素的“父元素”。
The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
这些元素位于XML树状结构文档内。树状结构起始于根元素,并向更低级别的树状分支结构扩展。
All elements can have sub elements (child elements):
文档中所有的元素都可以包含二级元素(即:子元素):
<root> <child> <subchild>.....</subchild> </child> </root> |
The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters).
这里提到的一些术语,如:父元素、子元素、同级元素使用与描述元素之间的相互关系的。父元素包含子元素;和子元素同级的称为同级元素(或兄弟、姐妹元素)。
All elements can have text content and attributes (just like in HTML).
所有的元素都包含文本内容和属性(这点和HTML极其类似)。
The image above represents one book in the XML below:
上述图表代表了下述XML文档中的一本书:
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
The root element in the example is <bookstore>. All <book> elements in the document are contained within <bookstore>.
案例中的根元素是<bookstore>。文档内所有的<book>元素都位于<bookstore>内。
The <book> element has 4 children: <title>,< author>, <year>, <price>.
与此同时,<book>元素还包含了4个子元素:<title>、< author>、<year>、<price>。