The second _xml/rss of XML simple tutorial

Source: Internet
Author: User
Tags reserved

Error in document Format
My mom's list contains dozens of recipes, or even hundreds of items. If you produce a
It's going to be very difficult to make mistakes-you'll be looking for missing tags line by row
Character. If you use several layers of nesting, it will be difficult to find errors.

But you can find a good help. Parser-XML code and report format error
Apps are available online for free. One of the best is lark, and it's made
By Tim Bray-xml is the standard technical editor and a strong advocate of the world's most
One of the smartest people.

I use Lark to analyze the following code. Note "Chocolate chips" and its closure
There is an error in the position of the marker in the </ingredients> marker:

<?xml version= "1.0"?>

<list>

<recipe>

<author>carol schmidt</author>

<recipe_name>chocolate Chip bars</recipe_name>

<meal>dinner

<course>Dessert</course>

</meal>

<ingredients>

<ITEM>2/3 C butter</item>

<item>2 C Brown sugar</item>

<item>1 tsp vanilla</item>

<item>1 3/4 C unsifted all-purpose flour</item>

<item>1 1/2 tsp baking powder</item>

<ITEM>1/2 tsp salt</item>

<item>3 eggs</item>

<ITEM>1/2 C chopped nuts</item>

<item>

</ingredients>2 Cups (12-oz pkg.) semi-sweet choc.

Chips</item>

<directions>

Preheat overn to 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. Stir in reserved dry

Ingredients, nuts, and chips.

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

Until golden brown; Cool. Cut into squares.

</directions>

</recipe>

</list>

The following is the result of the parser return:

Error

Line, column 22:encountered </ingredients> expected </item>

... assumed </item>

Line, column 36:encountered </item> with no start-tag.

With this information, finding errors will not be a problem. Then the validity of the XML file
What does it mean?

Implementation effectiveness
Eventually, we'll add information to the well-organized XML document. In fact, we
There's a lot to do-there's still a crisis lurking-although the XML file is well-organized,
However, critical information may also be lost. Take a look at the following example:

<recipe>
<author>carol schmidt</author>
<recipe_name>chocolate Chip bars</recipe_name>
<meal>dinner <course>Dessert</course> </meal>
<ingredients> </ingredients>
<directions>melt butter; Combine with, etc .... </directions>
</recipe>
This recipe does not contain ingredient, and because it is well organized,
The Lark Analyzer also does not detect problems. To manage even the most benign databases.
People know the mistakes we humans make: if we get the chance, we lose the key
Information and add useless nonsense. That's why the inventor of XML introduced a DTD--
The document type definition (DOC type definition). DTDs provide a way to protect
Proof XML is more or less the way you think.

Let's look at a DTD used on recipes.

<! DOCTYPE list [
<! ELEMENT recipe (recipe_name, author, meal, ingredients, directions) >
<! ELEMENT Ingredients (item+) >
<! ELEMENT Meal (#PCDATA, course?) >
<! ELEMENT Item (#PCDATA, sub_item*) >
<! ELEMENT recipe_name (#PCDATA) >
<! ELEMENT author (#PCDATA) >
<! ELEMENT Course (#PCDATA) >
<! ELEMENT Item (#PCDATA) >
<! ELEMENT Subitem (#PCDATA) >
<! ELEMENT directions (#PCDATA) >
]>
The code doesn't look friendly at first, but when you break it down, you can see
The meaning. Let us explain in detail:

<! DOCTYPE list [

This line is said to be included in square brackets with a document with the root element <list>
Dtd. As we mentioned before, the root element contains all the other elements.

<! ELEMENT recipe (Recipe_name, meal, ingredients, directions) >

This line defines the <recipe> tag character. Parentheses mean that four of these markers must be
Must appear in the <recipe> tag in order.

<! ELEMENT Meal (#PCDATA, course?) >

This line needs a detailed explanation. I have defined the following structure:

<meal>here the meal name is mandatory
<course>one course name may appear but it are not
Mandatory</course>
</meal>
I did it because, according to my idea, lunch is not necessarily a particular dish, but
It is possible to point out appetizers, main course and dessert after dinner. By specifying
#PCDATA-Represents parsed character data (that is, not binary data) to
Implement this function. Here, #PCDATA是文本-for example, "dinner".

The question mark after "course" indicates that the 0 or 1 pairs <course> markers will appear in <meal>
Tag.

Now let's take a look at the next line:

<! ELEMENT Ingredients (item+) >

The plus sign here indicates that at least one pair of <item> markers should appear in <ingredients>
Tag.

The last line we are interested in is:

<! ELEMENT Item (#PCDATA, sub_item*) >

I put sub_item* as a safety measure. In addition to requiring the text of each item
, I want to calculate the number of items per item. The asterisk is said in <item> mark
Can have the number of child entries. I don't need chocolate Chip bars recipes.
Any child entry, but it can be useful when its composition is complex.

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

Complete example of a DTD
The following is a complete example. I added another recipe to the file and
The DTD makes comments. Notice that I used the child entry in the second recipe.

<?xml version= "1.0"?>
<!--this starts the DTD. The four lines address document Structure-->
<! DOCTYPE list [
<! ELEMENT recipe (recipe_name, author, meal, ingredients,directions) >
<! ELEMENT Ingredients (item+) >
<! ELEMENT Meal (#PCDATA, course?) >
<! ELEMENT Item (#PCDATA, sub_item*) >
<!--These are the remaining elements of the recipe tag-->
<! ELEMENT recipe_name (#PCDATA) >
<! ELEMENT author (#PCDATA) >
<! ELEMENT directions (#PCDATA) >
<!--the remaining element of the meal tag-->
<! ELEMENT Course (#PCDATA) >
<!--the remaining element of the item tag-->
<! ELEMENT Sub_item (#PCDATA) >
]>
 

<?xml version= "1.0"?>
<list>
<recipe>
<author>carol schmidt</author>
<recipe_name>chocolate Chip bars</recipe_name>
<meal>dinner
<course>Dessert</course>
</meal>
<ingredients>
&LT;ITEM&GT;2/3 C butter</item>
<item>2 C Brown sugar</item>
<item>1 tsp vanilla</item>
<item>1 3/4 C unsifted all-purpose flour</item>
<item>1 1/2 tsp baking powder</item>
&LT;ITEM&GT;1/2 tsp salt</item>
<item>3 eggs</item>
&LT;ITEM&GT;1/2 C chopped nuts</item>
<item>2 Cups (12-oz pkg.) Semi-sweetchoc. Chips</item>
</ingredients>
<directions>
Preheat oven to 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.
Stir in reserved dry ingredients, nuts, and chips.
Spread in greased 13-by-9-inch pan.
Bake for 30minutes until golden brown; Cool.
Cut into squares.
</directions>
</recipe>
<recipe>
<recipe_name>pasta with Tomato sauce</recipe_name>
<meal>dinner
<course>Entree</course>
</meal>
<ingredients>
<item>1 lb spaghetti</item>
<item>1 16-oz can diced tomatoes</item>
<item>4 cloves garlic</item>
<item>1 Diced onion</item>
<item>italian Seasoning
<sub_item>oregano</sub_item>
<sub_item>basil</sub_item>
<sub_item>crushed Red pepper</sub_item>
</item>
</ingredients>
<directions>
Boil pasta. Sautégarlic and Onion.
Add tomatoes. Serve Hot.
</directions>
</recipe>
</list>
Now that you have a DTD, the document will be checked to see if it meets the limitations of the DTD. In other words
Say, we want to ensure the validity of the document.

To achieve this, we need another tool: the Validation analyzer. Microsoft
MSXML, a java-based program, easy to use and work well. Above the
The document was checked by this program and no errors were found. But if I check a
The ingredient tag does not contain a recipe for the entry, and the following information is returned:

Ingredients is not complete. Expected elements [item].

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.