Topics |
Step Six: Parsing your Valid XML DocumentThe goal of this step is to ensure that your document is well-formed and valid. If your document does not conform with XML rules or your DTD, it may not be read properly by an XML reader. Errors can ruin the work that has been put into a document and can require a great deal of work to remedy the problems if it is particularly error-dense. Most valid XML documents are created in validating XML editors, which are designed to avoid errors and often don't allow them. 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 and validity of your valid XML document is to use a parser, which checks for errors in your XML documents. Make sure you use a validating XML parser if you are checking valid XML documents. Parsers can be very helpful when you cannot find the reason why documents are not read properly. They are also a very good tool for checking your documents as you are creating them and before you use them. You might consider visiting the World Wide Web Consortium or Robin Cover's SGML and XML Pages 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 and validity of your document: NSGMLS -s xml.dcl yourfile.xml. Many other good parsers exist for this purpose. Good luck! Common Parsing ErrorsThese 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. Make sure that your markup also complies with your DTD, as validity is almost as important as well-formedness in valid XML documents. <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> |