Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Validate XML with JavaScript on an HTML5 Page

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.