参考:https://www.w3school.com.cn/xml/index.asp
示例:
<?xml version="1.0" encoding="UTF-8"?>
<messages>
<note id="501">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
<note id="502">
<to>John</to>
<from>George</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>
看上面的例子:
- 其中第一行是xml的文件头部,不是xml的数据内容,主要作用是用于识别xml文件。
- <messages>是根元素,这是xml中必须的一个元素,元素必须有一个开始和一个结束</messages>;
- <note id=“501”>是一个子元素,其中id是他的属性,请注意属性id的格式,id=号后面的属性内容必须用双引号包含,这个note子元素也有对应的结束符</note>;
- <note id=“502”>是message的子元素,是<note id=“501”>的兄弟(姐妹)元素;
- <to>George其中,to是note的子元素,George是to中的内容。
另外,需要注意:
- 元素的命名应该避免’-‘, ‘.’ ,’:'等符号。
- 元素的内容中’<‘, ‘>’, ‘&’, ’ ‘’ ‘, ’ " ‘,这几个符号需要分别被 ‘<’,’>’,’&’,‘&apos’,‘&quo’,替换;
- xml中的注释<!-- This is a comment -->
- 在xml中空格会保留