XML Tutorial: use an example to learn the XML syntax

Source: Internet
Author: User
The XML syntax is simple and strict. These rules are easy to learn and apply. As a result, it is not difficult to create software that can read and hold XML. Example of an XML document XML application a simple description

The XML syntax is simple and strict. These rules are easy to learn and apply.

As a result, it is not difficult to create software that can read and hold XML.

Example of an XML document

XML uses a simple syntax that can be described by the user.

 
 
  
   Tove
  
  
   Jani
  
  
   Reminder
  Don't forget me this weekend!
   

The first line in this document-XML declaration-defines the XML version and the character encoding used in the document. In this example, the XML 1.0 specification is followed and the ISO-8859-1 character set is applied.

The next line describes the root element of the document (like saying "this document is a note "):

   
 

The following four lines describe the four sub-elements (to, from, heading, and body) of the root element ):

 
  Tove
 
 
  Jani
 
 
  Reminder
 Don't forget me this weekend!  

The last line defines the end of the root element:

  

We can see that this XML document contains a note left by Jani to Tove. Now, you should approve our above view that XML has a comprehensive self-describing feature.

All elements must have closed labels.

When XML is applied, it is invalid to omit the closed tag.

In HTML, some elements do not have to have closed tags. The following code in HTML is justified:

This is a paragraph

This is another paragraph

In XML, all elements must have closed labels:

This is a paragraph

This is another paragraph

 

Note: You may have noticed from the above example that the XML declaration does not have a closed tag. This is not an error. Declarations are not part of XML. It is neither an XML element nor a closed tag.

XML tags are case sensitive.

Unlike HTML, XML tags are case sensitive.

In XML, tags And labels Is different.

Therefore, you must use the same case to open and close the tag:

 
  
This is incorrect.
 
 
  
This is accurate.
   
XML must be accurately nested

Inaccurate tag nesting does not make sense for XML.

In HTML, some elements can be embedded together with each other, just like this:

This text is bold and italic  

In XML, all elements must be accurately nested with each other, just like this:

This text is bold and italic  
XML documents must have root elements

All XML files must contain a single tag pair that can define the root element.

All other elements must be in the root element.

All elements can have child elements. Child elements must be accurately nested inside their parent elements:

   
      
   
    .....
     
  
    
The XML property value must be enclosed in quotation marks.

In XML, the quotation marks on both sides of the omitted attribute value are invalid.

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 accurate:

 
 
  
   Tove
  
  
   Jani
  
   
 
 
  
   Tove
  
  
   Jani
  
   

In the first document, the date attribute is not enclosed by quotation marks. This is accurate: date = "12/11/2002 ". This is incorrect: date = 12/11/2002.

In XML, spaces are retained.

In XML, spaces are not truncated.

This is different from HTML. In HTML, a sentence like this:

Hello              my name is Tove,  

It is displayed as follows:

Hello my name is Tove,  

This is because HTML splits multiple consecutive space characters into one.

In XML, CR/LF is converted to LF

In XML, a new Line (Line Feed) is stored as LF (Line Feed, Line Feed ).

Are you familiar with typewriter? Typewriter is a mechanical device used in the last century to produce printed documents. :-)

After you enter a line of text with the typewriter key, you need to manually move the slide to the left-side blank position and manually feed a line.

In the Windows Exploitation Program, the new line is often 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 exploitation programs, the new line is often stored as LF characters. While the Macintosh program only uses CR characters to store new lines.

Annotations in XML

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

   
XML is nothing special

XML is nothing special. It is just plain text with tags enclosed in angle brackets.

Software that can process plain text files can also process XML files. In a simple text editor, XML labels can also be displayed and are not particularly viewed.

In XML-aware, XML labels are specially processed. Depending on different types of exploitation programs, these labels may/may not be seen, and may have some functional significance.



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.