Simplified XML tutorial (2)

Source: Internet
Author: User
Tags xml file example xml parser
 
Ii. xml Basics
(1) XML terminology

Before learning XML, I would like to introduce some terms used in XML. We often encounter many terms that are hard to understand when learning new languages.

Let's first introduce some of the main topics.

Element: represents each component in the XML document. It contains characters, elements, or both. Empty element is allowed ).

Attribute (attributes): data placed after an element, representing another feature and feature of the element.

Unified encoding (UNICODE): A character set that includes useful characters in various languages around the world.

Entity: A piece of text. An entity is also an abbreviation. An entity can be a character or document data. It is the basic unit of XML text.

Markup: A component such as tag, entity reference, and Declaration.

Style Sheet (XSL): the language used to describe the format of XML Metadata files (because CSS is not enough for XML ).

PROLOG: information about the format of the document instance.

Document instance: organizes the actual document data in an element hierarchy.

(2) XML syntax Introduction

1. Let's look at a simple example.

This is a simple example of inventory item list. You can find many features of XML and understand that XML Metadata files are very clear and easy to use.

Read:

<! -XML file example -->

<? XML version = "1.0" encoding = "gb2312"?>

<Inventory list>

<! -- Here is the comment -->

<Inventory items>

<Name> towel </Name>

<Specification unit = "cm"> 25*50 </Specification>

<Manufacturer> China Cotton Mill </manufacturer>

<Production date> 20010-09-01 </production date>

<Quantity unit = "items"> 400 </quantity>

<Purchase price unit = "RMB"> 2.5 </import price>

<Retail price unit = "RMB"> 6.5 </retail price>

</Inventory items>

<Inventory items>

<Name> Bowl </Name>

<Specification unit = "cm"> 5*15 </Specification>

<Manufacturer> China enamel factory </manufacturer>

<Production date> 20010-09-11 </production date>

<Quantity unit = "count"> 200 </quantity>

<Purchase price unit = "RMB"> 1.5 </import price>

<Retail price unit = "RMB"> 4.5 </retail price>

</Inventory items>

<Inventory items>

<Name> chopsticks </Name>

<Specification unit = "items"> 1*10 </Specification>

<Manufacturer> China enamel factory </manufacturer>

<Production date> 20010-09-11 </production date>

<Quantity unit = "count"> 200 </quantity>

<Purchase price unit = "RMB"> 1.5 </import price>

<Retail price unit = "RMB"> 3.5 </retail price>

</Inventory items>

</Inventory list>

  

After reading the basic format of the XML language, I will explain in detail the components of the XML Metadata File. Actually, the above is very simple. Of course, I

To better understand and learn XML, you must clearly understand the meaning of these definitions.

2. Declaration)

The definition of an XML data file is not as free as HTML. It is very strict, and an error will occur if one file is missing! Because the first step of every XML parser is

Check whether the XML file is qualified, that is, "Well-formed". The first sentence is as follows:

<? XML version = "1.0" encoding = "gb2312"?>

Note that this sentence cannot be missed; otherwise, the parser will not parse it! Of course, encoding does not need to be written. The default value is Unicode. The character encoding for XML is used here.

I will explain the differences between Chinese and English. Some people have said that this is too simple. In fact, there is a difference between Chinese and English.

It's really troublesome. When I use antique dos, I know how difficult it is to use Chinese characters on a computer. Someone else can do it with 26 letters, but it takes tens of thousands of Chinese characters.

. Although the development of computer technology is fast and internationalized, it has always existed. Besides, it is not in English-speaking countries, so the major computer factories in the United States

Together with the ISO font standard, Unicode was introduced. This encoding can meet the needs of most languages!

3. elements and attributes (attributes)

<Element name attribute name = "attribute value">

<Child element> element data </child element>

</Element name>

As you can see in the above example, you can understand what an element is and what an attribute is. However, note that the elements are case-insensitive, that is, <

T> </T> and <t> </T> are not the same element. There is also the data in the element, as long as it is legal, it will be completely loyal to the data, as shown in:

<Element name> This Is A </element name>

<! -This is another -->

<Element name> This Is A </element name>

<! -This is another -->

<Element name> This Is

</Element name>

We didn't notice that there was only one space difference between the first and second, and the third was an additional carriage return, but the three data were different data, so everyone was writing this

Be careful when using some data!

If you are familiar with object orientation, you must know that classes also have attributes, and XML naturally includes attributes. XML elements have attributes and can

And the attribute values must be enclosed in single or double quotation marks.

4. Comments)

If you have written a program, you must know that it is very painful to look at other people's programs. If someone else's program has no comments, it will become

It is a terrible torment. Therefore, it is very helpful to embed information about documents and tags in documents. This not only helps others but also helps themselves.

In the XML annotation, the following is true:

<! -Ah, this is help -->

In the middle of this comment, we can place all characters except the "--" symbol, which is a bit unaccustomed. We often use this symbol to separate different components.

In the XML file, you need to change it.

5. predefined entities

When we create XML, some symbols such as "<" and so on are duplicated with XML, such as the following:

<Article>

<Paragraph>

<HTML>

<Head>

<Title> </title>

</Head>

<Body>

<H1> we are all alive Lei Feng.

<B> Lao Zhang drove to Northeast China ...... Hit </B>

</Body>

</Html>

</Paragraph>

</Article>

This is definitely not feasible. The definitions of HTML and XML are duplicated, Because XML has five predefined entities, as shown below:

&-------------------&

<---------------------- <

> --------------------->

& Apos ;------------------'

"------------------"

Then write these files as follows:

<Article>

<Paragraph>

<HTML>

<Head>

<Title> </title>

</Head>

<Body>

<H1> we are all alive Lei Feng.

<B> Lao Zhang drove to Northeast China ...... Hit </B>

</Body>

</Html>

</Paragraph>

</Article>

The parser will automatically convert these symbols into the files we want, but this is really hard to understand, so we have this CDATA.

6. CDATA

From the example in the above section, we can see that it is difficult to understand the use of pre-defined entities, but with this CDATA, we can let the analyzer put those special

Data is processed as normal data!

CDATA is defined as follows:

<! [CDATA [content]>

Use the example in the previous section as follows:

<Article>

<Paragraph> <! [CDATA [

<HTML>

<Head>

<Title> </title>

</Head>

<Body>

<H1> we are all alive Lei Feng.

<B> Lao Zhang drove to Northeast China ...... Hit </B>

</Body>

</Html>

]>

</Paragraph>

</Article>

In this way, the parser can explain the special characters in the middle of this symbol!

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.