Topics

Next topics

Step Four: Parsing your document

The goal of this step is checking that your well-formed XML document conforms to well-formed XML rules. If your document does not conform to these rules, it may not be read properly by an XML reader.

After you have invested your time in creating an XML document, you will expect it to behave properly. If your document is not completely well-formed, it might not act as desired. Ensure that every element has a start- and end-tag, particularly your root-element. All elements' start- and end-tags must nest properly within higher elements, meaning that elements cannot overlap, such as with the car and engine elements in the example below. Also check that all your attributes use single or double quotes and that each attribute value is contained by only one type of quotation marks.

A better, more precise way of checking the well-formedness of your XML is to use a parser, which checks for errors in your XML documents. They can be very helpful when you cannot find the reason why documents are not read properly. You might consider visiting the World Wide Web Consortium or Robin Cover's Validate/Check XML page for more information on parsers and anything else to do with XML. James Clark provides a handy parser, called NSGMLS, which is included in his SP and Jade packages. You can use the following NSGMLS command to check the well-formedness of your document: NSGMLS -s xml.dcl yourfile.xml. Many other good parsers exist for this purpose.

Parsing Examples

These parsing examples illustrate common mistakes and proper use of elements. The first two examples are well-formed XML, whereas the following ones would produce parsing errors, as they are not well-formed. Model your elements after the first two examples, obeying well-formed rules.

<PRICE>$57.80</PRICE> 
<PET><CAT type="Cornish Rex">Cat nests properly within PET.</CAT></PET> 
<WEATHER>Foggy 
<LEVEL>Intermediate<LEVEL> 
<PASSWORD>planetB612</PASSWD> 
<DISTANCE TYPE=KM 120</DISTANCE> 
<CAR><engine>engine does not nest properly within CAR</CAR></engine>