XML reference: xml basics (2)-XML tree structure and syntax rules

Source: Internet
Author: User
Tags xml attribute
XML Tree Structure

XML documents form a tree structure that begins with "root" and expands to "branches ".

An XML document instance

XML uses a simple self-descriptive Syntax:

 
<? XML version = "1.0" encoding = "ISO-8859-1"?> <Note> <to> George </to> <from> JOHN </from>  

The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/Western European character set ).

The following line describesRoot element(For example, "this document is a note "):

 
<Note>

The following four lines describe the fourChild Element(To, from, heading, and body ):

 
<To> George </to> <from> JOHN </from>  

The last line defines the end of the root element:

 
</Note>

In this example, we can imagine that the XML document contains a signature John gave George.

XML has excellent self-description. Do you agree?

XML documents form a Tree Structure

The XML document must containRoot element. This element is the parent element of all other elements.

The elements in the XML document form a document tree. The tree starts from the root and expands to the bottom of the tree.

All elements can have child elements:

 
<Root> <child> <subchild>... </subchild> </child> </root>

Terminologies such as parent, child, and compatriot are used to describe the relationship between elements. The parent element has child elements. Child elements at the same level become siblings ).

All elements can have text content and attributes (similar to HTML ).

Instance

Represents a book in the following XML:

<Bookstore> <book category = "Cooking"> <title lang = "en"> everyday Italian </title> <author> Giada De laurentiis </author> <year> 2005 </year> <price> 30.00 </price> </book> <book category = "children"> <title lang = "en"> Harry Potter </title> <author> j k. rowling </author> <year> 2005 </year> <price> 29.99 </price> </book> <book category = "Web"> <title lang = "en"> learning XML </title> <author> Erik T. ray </author> <year> 2003 </year> <price> 39.95 </price> </book> </bookstore>

In this example, the root element is <bookstore>. All <book> elements in the document are included in <bookstore>.

The <book> element has four child elements: <title>, <author>, <year>, and <price>.

 

XML syntax rules

XML syntax rules are simple and logical. These rules are easy to learn and use.

All XML elements must have close labels

In HTML, you will often see elements that do not close tags:

 
<P> This is a paragraph <p> This is another paragraph

In XML, it is invalid to omit and close tags. All elementsRequiredThere are closed labels:

 
<P> This is a paragraph </P> <p> This is another paragraph </P>

Note: You may have noticed that the XML declaration does not close the tag. This is not an error. Declarations are not part of XML. It is not an XML Element and does not need to close tags.

XML tags are case sensitive.

XML elements are defined using XML tags.

XML tags are case sensitive. In XML, labels <letter> and <letter> are different.

You must use the same case to write the Enable and disable tags:

 
<Message> This is incorrect. </Message> <message> This is correct. </Message>

Note: Open and Close tags are usually called start and end tags. No matter which term you like, they share the same concept.

XML must be correctly nested

In HTML, you will often see elements that are not correctly nested:

 
<B> <I> This text is bold and italic </B> </I>

In XML, all elements areRequiredCorrectly nest each other:

 
<B> <I> This text is bold and italic </I> </B>

In the preceding example, correct nesting means that the <I> element is opened in the <B> element, so it must be closed in the <B> element.

XML documents must have root elements

An XML document must have one element that belongs to all other elements.Parent Element. This element is calledRoot element.

 
<Root> <child> <subchild>... </subchild> </child> </root>
The XML property value must be enclosed in quotation marks.

Similar to HTML, XML can also have attributes (name/value pairs ).

In XML, XML Attribute values must be enclosed by quotation marks. Study the following two XML documents. The first is incorrect, and the second is correct:

 
<Note Date = 08/08/2008> <to> George </to> <from> JOHN </from> </Note>
 
<Note Date = "08/08/2008"> <to> George </to> <from> JOHN </from> </Note>

The error in the first document is that the date attribute in the note element is not enclosed by quotation marks.

Entity reference

In XML, some characters have special meanings.

If you put the character "<" in an XML element, an error occurs because the parser starts to treat it as a new element.

XML errors will occur:

 
<Message> If salary <1000 then </message>

To avoid this error, useEntity referenceTo replace the "<" character:

 
<Message> If salary& Lt;1000 then </message>

In XML, there are 5 predefined entity references:

& Lt; < Less
& Gt; > Greater
& Amp; & And number
& Apos; ' Single quotes
& Quot; " Quotation marks

Note: In XML, only the characters "<" and "&" are indeed invalid. It is a good habit to replace it with Entity references.

Annotations in XML

The syntax for writing comments in XML is similar to that in HTML:

<! -- This is a comment -->
In XML, spaces are retained

HTML will cut multiple consecutive space characters into one:

 
HTML: Hello my name is David. Output: Hello my name is David.

In XML, spaces in the document are not deleted.

Line feed with LF storage in XML

In Windows ApplicationsProgramA line feed is usually stored as a pair of characters: Carriage Return (CR) and line feed (LF ). This character is similar to the setting of a new line on the typewriter. In UNIX applications, new lines are stored as lf characters. The Macintosh application uses Cr to store new rows.

 

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.