XML's simplicity can make it easy to use in creating applications for online use, but like any markup language, it can fall prey to syntax and typing errors that halt your code in its tracks. Complex XML data and data structures raise the likelihood of error, particularly when you use significant numbers of attribute values. Successful XML error handling requires that you correct errors when they occur and adopt working practices that minimize their likelihood in the first place.
Validate your XML data. You may be tempted to worry about its accuracy only if it doesn't work within your project as a whole, but if you check your data while you program, you can save yourself stress when you use your code. The official W3C Markup Validation service (see Resources) provides the main source for validating XML markup. The W3Schools site (see Resources) offers a similar service, so choose one and upload your code. XML validation highlights errors in markup syntax. You may find that a single markup error produces multiple effects, increasing the apparent extent of a problem. For this reason, validate your XML data again after you repair each error.
Video of the Day
Highlight your XML data to help avoid XML errors during the development process. To create a preventive approach to XML errors, use a text editor that highlights XML syntax, such as Notepad++; a specialized XML editor, such as Oxygen or Amaya; or an integrated development environment, or IDE, such as Eclipse or Dreamweaver. Along with highlighting errors as you type, some of these tools prompt you with code corrections or examples, such as closing a tag you already opened or providing drop-down lists of tags within your document.
Check your markup against any XML Schema Definitions or Document Type Definitions provided with your XML data. These documents declare the structures, elements and attributes that a set of XML data should observe. Depending on the project, the Schema or DTD may be designed to enforce the rules necessary for usable XML data, along with other technologies involved in the application. Compliance failures can cause errors.
Check your XML structures for common markup errors. Valid XML requires that you close all tags. Check that your opening tags include closing tags with the same name or are self-closing, as follows:
Additionally, verify that you assign each of your element attributes a value and place these values between quotation marks. Your XML data structures must use correct nesting, so look for proper use of parent and child elements. The following code shows a nesting error because the child element closes outside the parent element instead of inside it:
Check for character errors, which often crop up in XML data. XML can use Unicode to accommodate a wide variety of international language characters. If you save an XML document using only ASCII encoding, some Unicode characters trigger errors. XML element and attribute values can't use some characters, including the ">" and "<" characters used to delineate tags:
When you use these characters within XML data, you must indicate them using their entity references, as follows:
Even if you're certain your code is clean, double check it to avoid losing time to additional error tracing.