XML Tree(树形结构)

xml文档(xml document)使用树形结构表达,以根结点root为开始,逐渐向下发散。它使用一种可自我描述的简单的语法.

一个xml文档分为文件序言(declaration)和文件主体两个大的部分。以figure 1为例,文件中的第一行即是文件序言,它是一个XML 文件必须要声明的东西,而且也必须位于XML 文件的第一行,它主要是告诉XML 解析器如何工作。接下来则是根元素root element(Figure 1里的note), 后面四行则是node的子节点child elements.而最后一行则是根节点完结标识。在xml里,每个element都需要有匹配的结束标记,如<note>与</note>,<to>与</to>,<body>与</body>...

 

Figure 1:

 

 <?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>

 

 

下面是一个xml文档,我们可以通过它构建一个xml结构示意图(figure 2). 在figure 2里,bookstore作为根元素,是整个树结构的起始端,所以book元素作为子元素(child),包含在它下面;而<title>,< author>, <year>, <price>,则是作为book元素的子元素,包含在book下面.通过这种方式,形成树形结构。

 

<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>

 

Figure 2: 

 

 

 要了解更多,可以访问:http://www.w3schools.com/xml/xml_tree.asp

 

 

posted on 2009-05-18 11:11  炜升  阅读(1978)  评论(0编辑  收藏  举报