XML tree structure

Een XML-document vormt een boomstructuur, die begint bij de 'wortel' en vervolgens uitbreidt naar de 'takken en bladeren'.

XML tree structure

DOM node tree

Voorbeeld van XML-document

Het diagram representerend deze XML-boeklijst:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="eten en drinken">
  <title lang="zh">Yaxie Talk Food</title>
  <author>Liang Shiqiu</author>
  <press>Jiangsu Literature and Art Publishing House</press>
  <year>2013</year>
  <price>35</price>
  <ISBN>9787539962771</ISBN>
</book>
<book category="kinderboek">
  <title lang="zh">De ongelooflijke vader van de vos</title>
  <author>Rolf Dahl</author>
  <translator> Dai Wei</translator>
  <press> Tomorrow Press</press>
  <year>2009</year>
  <price>10</price>
  <ISBN>9787533259563</ISBN>
</book>
<book category="literatuur">
  <title lang="zh">Van bekende naar onbekend</title>
  <author>Zigmund Bauman</author>
  <author>Peter Hafner</author>
  <translator>Wang Lique</translator>
  <press> Nanjing University Press</press>
  <year>2023</year>
  <price>68</price>
  <ISBN>9787305269387</ISBN>
</book>
<book category="wetenschap">
  <title lang="zh">Wil je vliegen, zoals een vogel?</title>
  <author>理查德·道金斯</author>
  <author>亚娜·伦佐娃</author>
  <translator>高天羽</translator>
  <press>湖南科学技术出版社</press>
  <year>2023</year>
  <price>88</price>
  <ISBN>9787571019075</ISBN>
</book>
<book category="政治" cover="精装">
  <title lang="zh">美国的民主论</title>
  <author>托克维尔</author>
  <translator>董果良</translator>
  <press>商务印书馆</press>
  <year>1989</year>
  <price>60</price>
  <ISBN>9787100124553</ISBN>
</book>
</bookstore>

XML tree structure

XML 文档中的元素形成了一棵元素树

这棵树从根元素开始,并扩展到树的子元素

XML 文档必须包含根元素。该元素是所有其他元素的父元素。

所有元素都可以有子元素:

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

父、子以及同胞等术语用于描述元素之间的关系。

父元素拥有子元素。子元素也有父元素。相同层级上的子元素成为同胞(兄弟或姐妹)。

所有元素均可拥有文本内容(雅舍谈吃)和属性(category="美食"),类似 HTML 中那样。

自描述的语法

XML 使用简单的具有自我描述性的语法:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>George</to>
<from>John</from>
<heading>Herinnering</heading>
<body>Don't forget the meeting!</body>
</note>

第一行是 XML 声明。它定义 XML 的版本 (1.0) 和所使用的编码 (ISO-8859-1 = Latin-1/西欧字符集)。

下一行描述文档的根元素(像在说:“本文档是一个便签”):

<note>

接下来 4 行描述根的 4 个子元素(to, from, heading 以及 body):

<to>George</to>
<from>John</from>
<heading>Herinnering</heading>
<body>Don't forget the meeting!</body>

The last line defines the end of the root element:

</note>

From this example, it can be imagined that the XML document contains a note from John to George.

XML has excellent self-descriptive properties, do you agree?