XML 示例
XML示例
XML文档创建的分层结构看起来像一棵树,因此它被称为XML树,它始于"根"并分支到"叶子"。
XML文档的示例
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>
第一行是XML声明。它定义了XML版本(1.0)和使用的编码(ISO-8859-1 = Latin-1/西欧字符集)。
下一行描述了文档的根元素(例如: "此文档为注释"):
接下来的4行描述了根的4个子元素(到,从,标题和主体)。
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
最后,最后一行定义了根元素的结尾。
XML文档必须包含
root元素。此元素是所有其他元素的"父元素"。
XML中的元素文档形成文档树。该树从根开始,并分支到树的最低层。
所有元素都可以包含子元素(子元素)。
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
术语"父代","子代"和"同级"用于描述元素之间的关系。父元素有孩子。处于同一级别的孩子称为兄弟姐妹(兄弟姐妹)。
所有元素都可以具有文本内容和属性(就像在HTML中一样)。
另一个示例的XML: 图书
文件: books.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>
示例中的根元素是
。全部
<书>
文档中的元素包含在
中。
书>
<book>元素有4个子元素: <title>,<year>和<price>。
另一个示例的XML: 电子邮件
文件: emails.xml
<?xml version="1.0" encoding="UTF-8"?>
<emails>
<email>
<to>Vimal</to>
<from>Sonoo</from>
<heading>Hello</heading>
<body>Hello brother, how are you!</body>
</email>
<email>
<to>Peter</to>
<from>Jack</from>
<heading>Birth day wish</heading>
<body>Happy birth day Tom!</body>
</email>
<email>
<to>James</to>
<from>Jaclin</from>
<heading>Morning walk</heading>
<body>Please start morning walk to stay fit!</body>
</email>
<email>
<to>Kartik</to>
<from>Kumar</from>
<heading>Health Tips</heading>
<body>Smoking is injurious to health!</body>
</email>
</emails>