简体   繁体   English

xml如何定义数据?

[英]xml how to define data?

In the xml, which way is better when define data in content or in the attribute ? 在xml中,在内容或属性中定义数据时哪种方法更好? Thanks 谢谢

<order id='' orderBy='' orderTime=''>
   .....
</order>

or 要么

<order id=''>
  .....
  <orderBy>.. </orderBy>
  <orderTime>...</orderTime>
</order>

Depends what the data will be. 取决于数据将是什么。 If the data are just simple strings then the first one will be fine. 如果数据只是简单的字符串,那么第一个就可以了。

The second one is important to use if you need to in the future wrap in CDATA . 如果将来需要包装CDATA则第二个使用的代码很重要。 So if the data is long or may contain html in the future then I suggest using the second form. 因此,如果数据太长或将来可能包含html ,则建议使用第二种形式。 If you look at twitter's XML feed , you will notice that they prefer using an element for almost all of their properties. 如果查看twitter的XML feed ,您会发现他们喜欢将元素用于几乎所有属性。 Using elements give you flexibility in the future if you need to add more multiple elements of the same type. 如果您需要添加更多多个相同类型的元素,则使用元素给您将来的灵活性。

Here is an article you should read that discusses this topic. 这是您应该阅读的讨论此主题的文章

My advice is to perhaps keep your id in an attribute but the rest in an element. 我的建议是将id保留在属性中,其余的保留在元素中。

In general, I prefer to use elements instead of attributes, as elements can later be expanded upon (the "extensible" part of XML). 通常,我更喜欢使用元素而不是属性,因为以后可以扩展元素(XML的“可扩展”部分)。 However, when you have a rather specific data type as with both of your examples here, you're probably best off keeping them as attributes - especially if orderTime is a standard XML dateTime type. 但是,当您有一个非常特定的数据类型(如此处的两个示例)时,最好保留它们作为属性-尤其是在orderTime是标准XML dateTime类型的情况下。

I would like to add one more point, if CDATA is not issue, then I prefer as attribute than Element. 我想补充一点,如果没有CDATA,那么我更喜欢作为属性而不是元素。 Order Id, date, count etc., should be attributes. 订单ID,日期,计数等应为属性。 Order Items should be Elements. 订单项应为元素。

Generally speaking, you use an attribute if: 通常,在以下情况下使用属性:

  • there is only one item (of that name) within the parent node 父节点中只有一个(该名称的)项

Otherwise, you use an element if: 否则,在以下情况下使用元素:

  • there can be more than one item (of that name) within the node, or 节点内可以有多个(该名称的)项,或者
  • the item is complex, ie, may contain attibutes or sub-elements itself 该项目很复杂,即本身可能包含服装或子元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM