Home

How to Validate XML with JavaScript on an HTML5 Page

|
|  Updated:  
2016-03-26 14:52:44
|   From The Book:  
No items found.
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

One of the strengths of XML is that it presents data in a highly structured manner making the context of the data understandable for JavaScript. However, one of the weaknesses of XML is that it doesn’t tolerate errors well. When a parser encounters an error in your XML file, it typically stops at that point and doesn’t perform any additional processing.

The concept of well-formed

Before you can use an XML document in any capacity, it must be well-formed. The problem is that most developers don’t speak XML, and you won’t use production data to gain experience working with XML in most cases. The files you create for experimentation purposes must be well-formed too.

Fortunately, you don’t have to invest in fancy software to check your work or rely on someone else’s eyes to find the mistakes you’ve made. A number of sites provide XML validators you can use for checking your work. Two such sites are:

In both cases, you simply copy and paste your XML into the window provided and click a button, and the application tells you about any errors in your file. Both sites also provide a means for working with complete files, rather than using the cutting and pasting approach.

XSD

The fact that an XML document is well-formed doesn’t say much. It’s easy to create a well-formed XML document that won’t work because the structure is incorrect. For example, the data might not include a required node, or the node might use the wrong capitalization or be misspelled.

All sorts of errors can creep into data files that aren’t validated in some way. The use of XML Schema Definition (XSD) files makes it possible to check the precise structure of a file and display a list of errors in it.

Using XSD is an important technique in situations where data errors of any sort could be catastrophic. (Think about banking or medical records.) You can gain a better understanding of XSD through the excellent W3Schools tutorial. The CodeGuru tutorial is shorter but provides helpful information on this technique as well.

About This Article

This article is from the book: 

No items found.

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.