Tuesday, April 26, 2011

How to Fix XML Errors


1. Validate your XML data. It can be tempting for developers to create XML data and only worry about its accuracy if it does not work within the project as a whole. However, by validating your XML data as a standard practice when you create it, you can save yourself stress when you use the data. The official W3C Markup Validation service is the main source for validating XML markup, yet the W3Schools site provides 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 has multiple knock-on effects, making the validation result seem worse than it is. For this reason, validate your XML data again after fixing each error.
2. Highlight your XML data. The best way to handle XML errors is to create a working situation in which they are avoided as much as possible during the development process. Use either a text editor that highlights XML syntax, such as Notepad++, a specialized XML editor, such as Oxygen or Amaya, or an IDE such as Eclipse or Dreamweaver to create a preventative approach to XML errors. As well as highlighting errors as you type, some of these tools prompt you with code, such as closing a tag you have already opened, or providing drop-down lists of tags within the document.
3. Use any XML Schemas or Document Type Definitions (DTD) provided. If your XML data is accompanied by an XML Schema Definition (XSD) or a DTD, use this to check your markup. These documents declare the structures, elements and attributes that a set of XML data is expected to observe. Depending on the project you're working on, the Schema or DTD may be designed to enforce the rules necessary for the XML data to be usable, along with other technologies involved in the application. Failure to comply with it may be causing errors.
4. Check your XML structures. When you are checking your XML for markup errors, there are a number of common things to look for. In valid XML, all tags must be closed, so check that your opening tags are either followed by a closing tag with the same name or are self-closing as follows:<object type='chair'/>It's also worth checking that all of your element attributes have been given a value, and that the values are contained between quotation marks. The structure of your XML data must also be nested correctly, so check that parent and child elements are structured properly. The following is an example of a nesting error, where the child element has been closed outside the parent element instead of inside it:<parent><child><name>Sam</name></parent></child>
5. Check for character errors. Character errors are common in XML data. XML is designed to contain a wide variety of international language characters, with Unicode the recommended encoding system to facilitate this. If an XML document has been saved using only ASCII encoding, the inclusion of some characters will cause errors. Some characters are also illegal when used within XML element and attribute values, so check for these also. They include the 'greater than' and 'less than' characters used to delineate tags:<tag>When used within XML data, these must be indicated using their entity references as in this example:<info>price > 500</info>