Intimate contact with XML (7)-Element vs. properties

Source: Internet
Author: User
Xml

You can store data in a child element or in an attribute. Take a look at the following examples:

< person sex= "female" >
< firstname>anna</firstname>
< lastname>smith</lastname>
</person>


< person>
< sex>female</sex>
< firstname>anna</firstname>
< lastname>smith</lastname>
</person>

In the first example, the gender sex is an attribute. In the second, sex is a child element. Two examples provide the same information. There are no special rules about when to use attributes and when to use child elements. My experience is that it's convenient to use attributes in HTML, but try to avoid using attributes in XML. If information is like data, use child elements.

The way I like it

I like to store data in child elements. The following 3 XML documents contain exactly the same information:

The first example uses a Date property:

< note date= "12/11/99" >
< to>tove</to>
< from>jani</from>
< heading>reminder< Body>don ' t forget me this weekend!</body>
</note>


A date element is used in the second example:

< note>
< date>12/11/99</date>
< to>tove</to>
< from>jani</from>
< heading>reminder< Body>don ' t forget me this weekend!</body>
</note>

An extended date element was used in the third one (this is my favorite method):

< note>
< date>
< day>12</day>
< month>11</month>
< year>99</year>
</date>
< to>tove</to>
< from>jani</from>
< heading>reminder< Body>don ' t forget me this weekend!</body>
</note>

Do you want to avoid using attributes?

Should you avoid using attributes? Here are some of the problems with using attributes:

Attribute cannot contain multiple values (while child elements can)
Attributes are not easily expanded (for future modifications)
property cannot describe structure (while child elements can)
Property is more difficult to manipulate by program code
Attribute values are not easy to test for DTDs
If you use attributes as a container for data, the end result is that documents will be difficult to read and maintain. You should try to use elements to describe the data. Attributes are used only when data-independent information is provided.

Don't end this way (if you think this is XML, then you don't really understand the point):

< note day= "month=" year= "99"
To= "Tove" from= "Jani" heading= "Reminder"
body= "Don ' t forget me this weekend!" >
</note>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.