Five Common XML Errors

Five Things You Should Never Do in XML

The XML (Extensible Markup Language) language is so simplistic that just about anyone can master it. That kind of accessibility is a key benefit of the language. The drawback to XML is that the rules that do exist in the language are absolute. XML parsers leave little room for error. Whether you are new to XML or have been working in the language for years, the same common errors tend to pop up over and over again. Let's take a look at five common mistakes people make when authoring documents in XML so that you can learn to avoid these missteps in your own work!

1
Forgetten Declaration Statement

Despite all their technical complexities, computers cannot yet think for themselves and use intuition to figure out what mean in different instances. You need to specify the language with a declaration statement, so that the browser understands the code that you will write. Forget this statement and the browser will have no idea which language you are using and will therefore be unable to do much with the code that you write.

2
Unnested Elements or Text

XML works in a hierarchical style. This means:

  • All child elements must have parents.
  • All data strings must be between element tags.
  • All comments must be inside comment tags.

3
Open Tags

XML requires you to close all of the tags that you open. A tag such as <book> requires </book> to close it. You cannot leave that open <book> just handing there! In HTML, you can get away with the occasional open tag, and some browsers will even close tags for you when they render a page. The document might still parse even if not well-formed. XML is much fussier than that. An XML document with an open tag will produce an error at some point.

4
No Root Element

Since XML works in a tree-structure, every XML page must have a root element at the apex of the tree. The name of the element is not important, but it must be there or the tags that follow will not be properly nested.

5
Multiple White-Space Characters

XML interprets 50 blank spaces the same it does one.

XML Code:                 Hello World!
Output: Hello World!

XML will take multiple blank spaces, known as white-space characters, and compact them into one space. Remember, XML is about carrying the data. It is not about the presentation of that data. It has nothing to do with visual display or design. A white space used to align text means nothing in XML code, so if you are adding lots of extra spaces to try to dictate some kind of visual layout or design, you are wasting your time.

Edited by Jeremy Girard