|
XML简易教程之二(1) 文档格式的排错 我妈妈_的清单中有数十条菜谱,甚至数百条。如果产生一个致 命错误,排错将非常困难 - 你将一行一行地寻找丢失的标记 符。如果使用几层嵌套,发现错误将很困难。
但是可以找到很好的帮助。分析器 - XML代码和报告格式错误 的应用程序可以在网上免费得到。其中最好的是Lark,它的作 者是由Tim Bray - XML规范的技术编辑和极力鼓吹者,地球上最 聪明的人之一。
我用Lark分析下面的代码。注意"chocolate chips"和它的关闭 标记符出现在</ingredients> 标记符中的位置有错误:
<?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 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.
</directions>
</recipe>
</list>
下面是分析器返回的结果:
Error Report
Line 17, column 22: Encountered </ingredients> eXPected </item>
... assumed </item>
Line 18, column 36: Encountered </item> with no start-tag.
有了这种信息,找到错误将不会成为问题。那么XML文件的有效性
|