Simple XML tutorial II

Source: Internet
Author: User
Troubleshooting of document structure: my mom's list contains dozens or even hundreds of recipes. If a fatal error is generated, the troubleshooting will be very difficult-you will search for the missing token line by line. If several layers of nesting are applied, it will be very difficult to invent the error.

Troubleshooting of document structure: my mom's list contains dozens or even hundreds of recipes. If a fatal error is generated, the troubleshooting will be very difficult-you will search for the missing token line by line. If several layers of nesting are applied, it will be very difficult to invent the error.

But it can be helpful. Analyzer-exploitation of XML code and report pattern errors can be obtained online for free. The best of them is Lark, which is written by Tim Bray-XML as a skillful compilation and propagandist, one of the smartest people on Earth.

I use Lark to analyze the following code. Note that 'Chocolate chips 'and its closed tag are displayed inThe status in the flag is incorrect:

Carol Schmidt

Chocolate Chip Bars

Dinner

Dessert

2/3 C butter

2 C brown sugar

1 tsp vanilla

1 3/4 C unsifted all-purpose flour

1 1/2 tsp baking powder

1/2 tsp salt

3 eggs

1/2 C chopped nuts

2 cups (12-oz pkg.) semi-sweet choc.

Chips

Preheat overn to 350 degrees. Melt butter;

Combine with brown sugar and vanilla in large mixing bowl.

Set aside to cool. Combine flour, baking powder, and salt; set aside.

Add eggs to cooled sugar mixture; beat well. Stir in reserved dry

Ingredients, nuts, and chips.

Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes

Until golden brown; cool. Cut into squares.

The results returned by the analyzer are as follows:

Error Report

Line 17, column 22: EncounteredExpected

... Assumed

Line 18, column 36: EncounteredWith no start-tag.

With this information, finding an error will not become a title. So what does XML file validity mean?

To achieve the ultimate effectiveness, we will participate in information in well-organized XML documents. In fact, we have a lot to do-there is still a crisis ambush-although XML files are well organized,
However, critical information may be lost. Take a look at the following example:


Carol Schmidt
Chocolate Chip Bars
Dinner Dessert

Melt butter; combine with, etc ....

This recipe does not contain ingredient, and because it is well organized, Lark analyzer does not invent titles. Those who have managed even the most amiable database know the common mistakes we humans make: if there is a chance, we will lose the key information and participate in useless empty talk. This is why the inventor of XML introduced the Document Type Definition (Document Type Definition ). DTD provides a way to ensure that XML is more or less what you think.

Let's look at a DTD used in recipes.











]>
These codes seem unfriendly at first, but they can be seen when they are broken down. Let's explain in detail:

This line means that the square brackets contain a root element. The DTD of a document. For example, the root element contains all other elements.

This line defines Identifier. Parentheses indicate that four of the four tags must be displayed in order Mark.

This line must be described in detail. I have defined the following structure:

Here the meal name is mandatory
One course name may appear, but it is not
Mandatory

I did this because, according to my idea, lunch is not necessarily a specific dish, but dinner may have to point out appetizing food, main course and post-dinner dessert. You can specify # PCDATA to present the parsed character data (that is, non-binary data. Here, # PCDATA is text-for example, "dinner ".

The question mark after 'course' is 0 or 1 pair. The token will be displayed in .

Now let's look at the next line:

Here, the plus sign represents at least one pair. The identifier should be displayed in .

The last line we like is:

I use sub_item * as a security measure. In addition to the text of each item, I hope to check the number of content of each item. The asterisk indicates The tag can contain the number of sub-entries. I don't need any sub-entries in the Chocolate Chip Bars recipe, but it is needed when its composition is complex.

Now let's put these together to see what we can get.

Complete DTD example
The following is a complete example. I added another recipe to the document and commented on the DTD. Note that I used subentries in the second recipe.















]>
 




Carol Schmidt
Chocolate Chip Bars
Dinner
Dessert


2/3 C butter
2 C brown sugar
1 tsp vanilla
1 3/4 C unsifted all-purpose flour
1 1/2 tsp baking powder
1/2 tsp salt
3 eggs
1/2 C chopped nuts
2 cups (12-oz pkg.) semi-sweetchoc. chips


Preheat oven to 350 degrees. Melt butter;
Combinewith brown sugar and vanilla in large mixing bowl.
Set aside to cool. Combine flour, baking powder, andsalt;
Set aside. Add eggs to cooled sugar mixture; beat well.
Stir in reserved dry ingredients, nuts, and chips.
Spread in greased 13-by-9-inch pan.
Bake for 25 to 30 minutes until golden brown; cool.
Cut into squares.



Pasta with tomato Sauce
Dinner
Entree


1 lb spaghetti

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.