wood burning stoves*
The moose likes XML and Related Technologies and the fly likes validate xml dom with schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "validate xml dom with schema" Watch "validate xml dom with schema" New topic

validate xml dom with schema

Harlan Vossos

Joined: Jun 13, 2001
Posts: 1
I need to validate a xml document already loaded into a dom against a xml schema. How do you do this.
ps. I am using xerces xml parser.
Johannes Hartman

Joined: May 07, 2001
Posts: 13

I am no expert at this, but as far as my knowledge go I do not believe that is available at this moment. The schema recommendation was not officially finilized until May (If I recall correctly) of this year, and since schema went through so much change before the finilization, most tools only provide partial support for schemas. My best suggestion is "hang on" - I'm sure it will be available sometime soon here.
Otherwise, if you have to get on with it, maybe you could use DTD's for the time being? If this is not an option, how critical is performance having to go back and forth between the DOM and an XML file/stream?
Johannes Hartman

Joined: May 07, 2001
Posts: 13
Check out these schema validators - I do not know whether any of them are actual in-memory validators, but if not, maybe you could get a workaround....
Xerces Java Parser:
IBM XML Schema Quality Checker: www.alphaworks.ibm.com/tech/xmlsqc
XML Scema Validator (XSV): www.ltg.ed.ac.uk/~ht/xsv-status.html
Let me know if you solved your problem....
Manuel Palacio
Ranch Hand

Joined: Oct 16, 2000
Posts: 45
What's the trick to validate an XML doc against a schema with Xerces? I can't make it work. Not even with the example program SAXCount with validation turned on.
The docs I'm using are the ones in the example that comes with xerces: personal-schema.xml and personal.xsd
And all I get is :
[Error] personal-schema.xml:4:48: Element type "personnel" must be declared.
[Error] personal-schema.xml:4:48: Attribute "xmlns:xsi" must be declared for element type "personnel".
...etc. Sounds like it can't find the schema or something but it's in the same directory.
I would really appreciate if you could tell me what I'm doing wrong! Thanks.

<a href="http://www.newinstance.net" target="_blank" rel="nofollow">http://www.newinstance.net</a>
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
Can you post these two files here?
Manuel Palacio
Ranch Hand

Joined: Oct 16, 2000
Posts: 45
Sure-- The XML doc
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<link manager="Big.Boss"/>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<link manager="Big.Boss"/>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<link manager="Big.Boss"/>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<link manager="Big.Boss"/>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<link manager="Big.Boss"/>
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
<element name="personnel">
<element ref="person" minOccurs='1' maxOccurs='unbounded'/>
<unique name="unique1">
<selector xpath="person"/>
<field xpath="@name"/>
<key name='empid'>
<selector xpath="person"/>
<field xpath="@id"/>
<keyref name="keyref1" refer='empid'>
<selector xpath="person"/>
<field xpath="link/@manager"/>
<element name="person">
<element ref="name"/>
<element ref="email" minOccurs='0' maxOccurs='unbounded'/>
<element ref="url" minOccurs='0' maxOccurs='unbounded'/>
<element ref="link" minOccurs='0' maxOccurs='1'/>
<attribute name="id" type="ID" use='required'/>
<attribute name="note" type="string"/>
<attribute name="contr" default="false">
<restriction base = "string">
<enumeration value="true"/>
<enumeration value="false"/>
<attribute name="salary" type="integer"/>
<element name="name">
<element ref="family"/>
<element ref="given"/>
<element name="family" type='string'/>
<element name="given" type='string'/>
<element name="email" type='string'/>
<element name="url">
<attribute name="href" type="string" default="http://"/>
<element name="link">
<attribute name="manager" type="IDREF"/>
<attribute name="subordinates" type="IDREFS"/>
<notation name='gif' public='-//APP/Photoshop/4.0' system='photoshop.exe'/>
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
I tried to write a simple class to parse your file.
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.apache.xerces.framework.XMLParser;
import org.apache.xerces.parsers.SAXParser;
import java.io.*;
* A sample SAX2 counter. This sample program illustrates how to
* register a SAX2 ContentHandler and receive the callbacks in
* order to print information about the document.
* @version $Id: SAX2Count.java,v 1.6 2001/05/17 21:04:17 neilg Exp $
public class SAXTest {
public static void main( String args[] ) {
try {
XMLParser parser = new SAXParser();
//set entity resolver, so always use the registered schema
parser.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// use the default behaviour
return new InputSource( "personal.xsd" );
//set error handler and validation feature to true
parser.setErrorHandler(new DefaultHandler() {
public void error(SAXParseException e) throws SAXParseException {throw e; }
public void fatalError(SAXParseException e) throws SAXParseException {throw e; } public void warning(SAXParseException e) throws SAXParseException {
System.out.println( e.toString() );
parser.setFeature( "http://xml.org/sax/features/validation", true);
long before = System.currentTimeMillis();
parser.parse( "personal.xml" );
long after = System.currentTimeMillis();
System.out.println(after - before);
} catch (org.xml.sax.SAXParseException spe) {
} catch (org.xml.sax.SAXException se) {
if (se.getException() != null)
} catch (Exception e) {
The message I got is:
org.xml.sax.SAXParseException: The content of element type "name" must match "all(family,given)".
at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1196) at org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java, Compiled Code) at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java, Compiled Code) at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java, Compiled Code)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1081)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1122)
at SAXTest.main(SAXTest.java:98)
So it seems like it's doing the job. I am using xerces 1.4.1. Try the code above.
sateesh arumbaka

Joined: Nov 22, 2002
Posts: 6
Downloaded xerces 1.4.4 and am trying to run SAXCount and DOMCount with validation "on"....
its throwing some errors... like
"[Error] personal-schema.xml:3:50: Element type "personnel" must be declared."
what other settings,,, if any do I need to make/ take care of..
basically I want to run an XML file across a schema and see if the XML file is valid or not...
Thanks in advance..
I agree. Here's the link: http://aspose.com/file-tools
subject: validate xml dom with schema
Similar Threads
XML Schema Validation
how to find schema of an xml from dom program at run time
How to turn on Xerces validation? Help!!!
need 2 know..
Howto validate xml schema?