The basics of XML development: XML elements

Source: Internet
Author: User
Tags xml example

An XML document contains XML elements.

What is an XML element?

An XML element refers to the portion of a (and including) start tag that is (and includes) the end tag.

An element can contain other elements, text, or a mixture of both. Elements can also have attributes.

<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>LearningXML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

In the example above, both,<bookstore> and <book> have element content because they contain other elements. <author> only text content, because it contains only text.

In the example above, only the <book> element has attributes (category= "CHILDREN").

XML naming rules

XML elements must follow the following naming rules:

Names can contain letters, numbers, and other character names cannot start with numbers or punctuation names cannot start with character "xml" (or XML, XML) names cannot contain spaces

You can use any name without a reserved word.

Best naming habits

Makes a name descriptive. It's also nice to use the underlined name.

The name should be shorter, such as: <book_title>, rather than:<the_title_of_the_book>.

Avoid the "-" character. If you name it this way: "First-name", some software will think you need to extract the first word.

Avoid the "." Character. If you name it this way: "First.name", some software will think that "name" is the property of object "a".

Avoid the ":" character. The colon is converted to a namespace to use (described later).

XML documents often have a corresponding database with fields that correspond to the elements in the XML document. There is a practical experience in naming an element in an XML document using the name rules of the database.

Non-English letters such as Éòá are also valid XML element names, but pay attention to the problems that may occur when software developers do not support these characters.

XML elements are extensible

XML elements are extensible to carry more information.

Take a look at the following XML example:

<note>
<to>George</to>
<from>John</from>
<body>Don't forget the meeting this weekend!</body>
</note>

Let's imagine that we created an application that extracts the <to>, <from>, and <body> elements and produces the following output:

MESSAGE
To: George
From: John
Don't forget the meeting this weekend!

Imagine, then, that the XML document author added some additional information to the document:

<note>
<date>2008-08-08</date>
<to>George</to>
<from>John</from>
<body>Don't forget the meeting this weekend!</body>
</note>

So will this application break or crash?

No. The application can still find the <to>, <from>, and <body> elements in the XML document and produce the same output.

One of the advantages of XML is that it can be extended frequently without disrupting the application.

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.