Click here to Skip to main content
13,148,136 members (35,512 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 26 Aug 2010

Validating XML Document using XML Schema using XMLBeans

, 26 Aug 2010
Rate this:
Please Sign up or sign in to vote.
XMLBeans for Java can be used for validating XML Document based on some schema


XML documents are normally validated using DTD or XML schemas. XML Schemas are simpler and yet more powerful to validate any XML document. Java has an inbuilt Xerces parser which can be used for validation of any XML doc against a schema but it tends to be slow. XMLBeans is a Java XML library to perform operations like parsing, validating, marshalling, unmarshalling, etc.

This article demonstrates how we can validate XML doc against XML schema using XMLBeans.


A very good tutorial for XML schema is available at W3Schools. XMLBeans is available here. The library can be used to validate an XML document against multiple schema documents.

Using the Code

The sample code has been developed using Maven and all you need is to download maven and install to get the application running.

From the project folder run command:

mvn clean install

The following line of code parses the XML schema document itself and loads it:

XmlObject parsedSchema = XmlObject.Factory.parse(schemaStream , 
new XmlOptions().setLoadLineNumbers().setLoadMessageDigest());

This is how you create a Schema Type System for validation:

SchemaTypeLoader loader = XmlBeans.compileXsd(schemas, null, 
   new XmlOptions().setErrorListener(null).setCompileDownloadUrls().

This is how you parse the XML document using the type system to get XML object:

XmlObject object = loader.parse(xmlStream, 

null, new XmlOptions().setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT));

And finally the validation:

object.validate(new XmlOptions().setErrorListener(errorList)); 

To invoke the validation, we need to pass 3 arguments:

  1. The schema file
  2. The XML document
  3. ArrayList<String> to trap the errors during validation

Also note that we can use multiple schemas to validate but this example only shows validation using one schema.

The schema or document can be provided from a variety of sources including file, inputstream or XML Node.


  • 26th August, 2010: Initial post


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

anand kr
Software Developer (Senior)
India India
Specialization in Java/J2EE/JSF/Richfaces/Spring/Spring Integration
Area of interest: Refactoring with design patterns.
Currently working on Spring Integration and ActiveMQ.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Emilio Garavaglia25-Aug-10 19:38
memberEmilio Garavaglia25-Aug-10 19:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.1 | Last Updated 26 Aug 2010
Article Copyright 2010 by anand kr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid