首页 > 其他 > 详细

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

时间:2018-03-15 15:54:38      阅读:151      评论:0      收藏:0      [点我收藏+]

我最喜欢的方式

下面的三个 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>

 

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

原文:https://www.cnblogs.com/dlsunf/p/8573921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!