我最喜欢的XML(三种方式)

我最喜欢的方式

下面的三个 XML 文档包含完全相同的信息:

第一个例子中使用了 date 属性:

<note date="08/08/2008">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note> 

第二个例子中使用了 date 元素:

<note>
<date>08/08/2008</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note> 

第三个例子中使用了扩展的 date 元素(这是我的最爱):

<note>
<date>
  <day>08</day>
  <month>08</month>
  <year>2008</year>
</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

避免 XML 属性?

因使用属性而引起的一些问题:

  • 属性无法包含多重的值(元素可以)
  • 属性无法描述树结构(元素可以)
  • 属性不易扩展(为未来的变化)
  • 属性难以阅读和维护

尽量使用元素来描述数据。而仅仅使用属性来提供与数据无关的信息

不要做这样的蠢事(这不是 XML 应该被使用的方式):

<note day="08" month="08" year="2008"
to="George" from="John" heading="Reminder" 
body="Don't forget the meeting!">
</note>
原文地址:https://www.cnblogs.com/dlsunf/p/8573921.html