XML Document Validation with an XML Schema
by Deepak Vohra09/15/2004
An XML schema defines the structure of the elements and attributes in an XML document. For an XML document to be valid based on an XML schema, the XML document has to be validated against the XML schema. This tutorial explains the procedure of validating an XML document with an XML schema.
In this article, the Xerces2-j and JAXP parsers
are used to validate an XML document with an XML schema. In
Xerces2-j, schema validation is integrated with the
SAXParser and DOMParser parsers. In
JAXP, DocumentBuilder classes are used to
validate a XML document. XML schema validation is illustrated with an
XML document comprising of a catalog. This article is structured into
the following sections:
- Preliminary Setup
- Overview
- Validation of an XML Document with the Xerces2-j Parser
- Validation of an XML Document with the JAXP Parser
Preliminary Setup
To validate an XML document with the Xerces2-j parser,
the Xerces2-j classes need to be in the classpath.
The Xerces2-j parser may be obtained from the Xerces2-j page.
Extract the Xerces-J-bin.2.5.0.zip (for Windows) or
Xerces-J-bin.2.5.0.tar.gz (for Unix) files to the
installation directory of your choice.
Add <XERCES>/xerces-2_5_0/xercesImpl.jar
and <XERCES>/xerces-2_5_0/xml-apis.jar to the
classpath variable, where <XERCES>is the directory in
which Xerces2-j is installed.
To validate a XML document with the JAXP parser, its
DocumentBuilder classes need to be in the classpath. These
are provided by the Java Web Services Developer Pack, which may be
obtained from the JWSDP web site.
Extract the Java Web Services Developer Pack 1.2 (jwsdp-1.2) application
file to an installation directory. Add <JAXP>/jaxp/lib/jaxp-api.jar and
<JAXP>/jaxp/lib/endorsed/xercesImpl.jar to the classpath variable, where <JAXP> is the directory in which you installed jwsdp-1.2.
Overview
In this tutorial, an example XML document named catalog.xml, consisting of an ONJava journal catalog, is used. The xmlns:xsi attribute, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",
defines the XML namespace prefix, xsi. The xsi:noNamespaceSchemaLocation attribute,
xsi:noNamespaceSchemaLocation="file://c:/Schemas/catalog.xsd", defines the schema for elements in the XML document without a namespace prefix. The example XML document is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<!--A OnJava Journal Catalog-->
<catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"file://c:/Schemas/catalog.xsd"
title="OnJava.com" publisher="O'Reilly">
<journal date="April 2004">
<article>
<title>Declarative Programming in Java</title>
<author>Narayanan Jayaratchagan</author>
</article>
</journal>
<journal date="January 2004">
<article>
<title>Data Binding with XMLBeans</title>
<author>Daniel Steinberg</author>
</article>
</journal>
</catalog>
The example XML document is validated with an example XML schema file,
catalog.xsd. The elements in this schema document are in
the XML schema namespace of http://www.w3.org/2001/XMLSchema. The catalog.xsd file
looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="journal" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="title" type="xs:string"/>
<xs:attribute name="publisher" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="journal">
<xs:complexType>
<xs:sequence>
<xs:element ref="article" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="date" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="article">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author" type="xs:string"/>
</xs:schema>
In the following sections, we'll discuss validation of the example XML document, catalog.xml, with the example schema document, catalog.xsd.
Validation of an XML Document with the Xerces2-j Parser
Xerces2-j provides the DOMParser and the
SAXParser for parsing an XML document. To use SAX parsing,
import the SAXParser.
import org.apache.xerces.parsers.SAXParser;
A DefaultHandler is used as the
ErrorHandler with the parser. An ErrorHandler
registers the errors generated by the parser. Import the
DefaultHandler class.
import org.xml.sax.helpers.DefaultHandler;
To validate with a SAXParser, create a
SAXParser. The SAXParser class is a subclass
of the XMLParser class.
SAXParser parser = new SAXParser();
Set the validation feature to true to report validation
errors. If the validation feature is set to true, the XML
document should specify a XML schema or a DTD.
parser.setFeature("http://xml.org/sax/features/validation",
true);
Set the validation/schema feature to true to report
validation errors against a schema.
parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
Set the validation/schema-full-checking feature to true to
enable full schema, grammar-constraint checking.
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
Specify a validation schema for the parser with the
schema/external-noNamespaceSchemaLocation or the
schema/external-schemaLocation property. The
schema/external-schemaLocation property is used to specify
a schema with a namespace. A schema list may be specified with the
schema/external-schemaLocation property. The
schema/external-noNamespaceSchemaLocation property is
used to specify a schema that does not have a namespace. A parser is
not required to locate a schema specified with the
schema/external-noNamespaceSchemaLocation and
schema/external-schemaLocation properties. For our purposes,
a schema without a namespace is used to validate an XML document.
parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
SchemaUrl);
Create a class that extends the DefaultHandler class.
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception)
throws SAXException { }
}
The DefaultHandler class implements the
ErrorHandler interface, and is used to specify an
ErrorHandler for the Xerces parser. Instantiating the
above Validator class allows us to register it as an
ErrorHandler with the parser.
Validator handler = new Validator();
parser.setErrorHandler(handler);
Since Validator implements ErrorHandler,
you can use it to parse the example XML document. The parse methods
parse(java.lang.String systemId) and
parse(org.xml.sax.InputSource inputSource) may be used for
parsing an XML document.
parser.parse(XmlDocumentUrl);
The errors generated by the parser get registered with the
ErrorHandler and are retrieved from the
ErrorHandler. The example program, SchemaValidator.java
(see Resources below), is used to validate the example XML document, catalog.xml, with the example XML schema, catalog.xsd.
String variables such as SchemaUrl and XmlDocumentUrl
are specified as file URLs. For example:
SchemaUrl: file://c:/schema/catalog.xsd
XmlDocumentUrl: file://c:/catalog/catalog.xml.
Validation of an XML Document with the JAXP Parser
Another way to validate an XML document is with a JAXP
DocumentBuilder. To begin, import the
DocumentBuilderFactory and DocumentBuilder classes.
The DocumentBuilder class is used to obtain a
org.w3c.dom.Document document from an XML document, while the
DocumentBuilderFactory class is used to obtain a
DocumentBuilder parser.
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
To validate with a DocumentBuilder parser, set the System property
javax.xml.parsers.DocumentBuilderFactory:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
Next, you need to create a DocumentBuilderFactory.
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
An instance of DocumentBuilderFactory is found by applying
the following rules and taking the first one that succeeds:
- Use the
javax.xml.parsers.DocumentBuilderFactorysystem property. - Use the properties file lib/jaxp.properties in the JRE directory.
- Use the META-INF/services/javax.xml.parsers.DocumentBuilderFactory file with the Services API.
- Use the Platform default
DocumentBuilderFactoryinstance.
To parse a XML document with a namespace, set the
setNamespaceAware() feature to true. By
default, the setNamespaceAware() feature is set to
false.
factory.setNamespaceAware(true);
Set the setValidating() feature of the
DocumentBuilderFactory to true to make the parser a validating parser.
By default, the setValidating() feature is set
to false.
factory.setValidating(true);
Set the schemaLanguage and schemaSource
attributes of the DocumentBuilderFactory. The
schemaLanguage attribute specifies the schema language for
validation. The schemaSource attribute specifies the XML
schema document to be used for validation.
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
SchemaUrl);
Create a DocumentBuilder parser.
DocumentBuilder builder = factory.newDocumentBuilder();
This returns a new DocumentBuilder, with the parameters configured in
the DocumentBuilderFactory. Create
and register an ErrorHandler with the parser.
Validator handler=new Validator();
builder.setErrorHandler(handler);
Validator is a class that extends the
DefaultHandler class. The DefaultHandler class
implements the ErrorHandler interface. The
Validator class is listed in the previous section. Parse
the XML document with the DocumentBuilder parser. The
different parse methods are parse(InputStream is),
parse(File f), parse(InputSource is),
parse(InputStream is,String systemId),
and parse(String uri).
builder.parse(XmlDocumentUrl);
Validator, an ErrorHandler of the type
DefaultHandler, registers errors generated by the
validation. The example program, JAXPValidator.java
(see Resources below), is used to validate
the example XML document, catalog.xml, with the example XML schema,
catalog.xsd, using the JAXP parser.
Conclusion
For an XML document to be based on an XML schema,
the XML document is required to be validated with the schema. This
tutorial explained the validation of an example XML document with an
example XML schema with a Xerces2-j parser, and the
JAXP DocumentBuilder parser.
Resources
- Sample code for this article.
- Xerces2-j
- Java Web Services Developer Pack
Deepak Vohra is a NuBean consultant and a web developer.
Return to ONJava.com.
Showing messages 1 through 62 of 62.
-
Digester with xerces
2007-10-01 00:48:22 rayon [Reply | View]
-
Digester with xerces
2007-10-01 10:07:37 Deepak Vohra [Reply | View]
normalize-attribute-values
is not supported by Xerces.
The features that are supported are:
http://xerces.apache.org/xerces-j/features.html
-
Digester with xerces
2007-10-01 00:48:11 rayon [Reply | View]
Hi,
I use Digester version 1.5 and xerces
i defined default in my xsd. when parsing the xml i don't get them.
Code:
digester.setFeature("http://xml.org/sax/features/validation",true);
digester.setFeature("http://apache.org/xml/features/validation/schema",true);
digester.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
digester.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","Statistics.xsd");
The feature ://digester.setFeature("http://apache.org/xml/features/validation/normalize-attribute-values",true); is not regognized by xerces.
What can i do?
-
what about "external-schemaLocation"?
2007-05-04 03:22:02 gneidisch [Reply | View]
All the examples are with schemas with no NameSpace.
Can anybody provide an example of the use of the propertyhttp://apache.org/xml/properties/schema/external-schemaLocation
??
I thought it'd be similar to the use of the "external-noNamespaceSchemaLocation
" property, but it actually differs. A SAXParser example would be greatly appreciated.
-
what about "external-schemaLocation"?
2007-05-04 05:39:53 Deepak Vohra [Reply | View]
Refer
http://forum.java.sun.com/thread.jspa?threadID=516385&messageID=2466823 -
what about "external-schemaLocation"?
2007-05-04 05:37:09 Deepak Vohra [Reply | View]
http://www-128.ibm.com/developerworks/xml/library/x-tipschnm.html
Specify namespace schemas with xsi:schemaLocation
-
Error coming
2007-01-09 14:15:25 javanew [Reply | View]
Hello Deepak, when I try to use catalog.xml and catalog.xsd, i get the following errors. Can you please give me some idea on why this error could be coming:
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document '<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="catalog"> <xsd:complexType> <xsd:sequence> <xsd:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="title" type="xsd:string"/> <xsd:attribute name="publisher" type="xsd:string"/> </xsd:complexType> </xsd:element> <xsd:element name="journal"> <xsd:complexType> <xsd:sequence> <xsd:element ref="article" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="date" type="xsd:string"/> </xsd:complexType> </xsd:element> <xsd:element name="article"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element ref="author" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="author" type="xsd:string"/></xsd:schema>', because
1) could not find the document;
2) the document could not be read;
3) the root element of the document is not <xsd:schema>. -
Error coming
2007-01-09 14:57:47 Deepak Vohra [Reply | View]
Is the schema specified in the XML document?
Please specify schema in XML document withn noNamespaceSchemaLocation attribute. -
Error coming
2007-01-09 15:19:41 javanew [Reply | View]
Now I specified specifying noNamespaceSchemaLocation attribute. Now my xml looks like this. Both xml and xsd are WSAD workspace, so I cannot give file path. There are in the same location. So, do i need to mention any relative path?
<?xml version="1.0" encoding="utf-8" ?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="catalog.xsd" >
<journal date="April 2004">
<article>
<title>Declarative Programming in Java</title>
<author>Narayanan Jayaratchagan</author>
</article>
</journal>
<journal date="January 2004">
<article>
<title>Data Binding with XMLBeans</title>
<author>Daniel Steinberg</author>
</article>
</journal>
</catalog> -
Error coming
2007-01-09 15:08:32 javanew [Reply | View]
i was not specifying noNamespaceSchemaLocation attribute. My xml looked like this:
<?xml version="1.0" encoding="utf-8" ?>
<catalog xmlns="catalog.xsd">
<journal date="April 2004">
<article>
<title>Declarative Programming in Java</title>
<author>Narayanan Jayaratchagan</author>
</article>
</journal>
<journal date="January 2004">
<article>
<title>Data Binding with XMLBeans</title>
<author>Daniel Steinberg</author>
</article>
</journal>
</catalog> -
Error coming
2007-01-09 15:13:51 Deepak Vohra [Reply | View]
Please modify the root element.
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://c:/Schemas/catalog.xsd"> -
Error coming
2007-01-09 15:13:04 Deepak Vohra [Reply | View]
Please modify the root element.
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://c:/Schemas/OracleCatalog.xsd"> -
Error coming
2007-01-09 15:26:03 javanew [Reply | View]
Both xml and xsd are WSAD workspace, so I cannot give file path. There are in the same location. So, is there any way I can specify the path?
<?xml version="1.0" encoding="utf-8" ?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="catalog.xsd" >
<journal date="April 2004">
<article>
<title>Declarative Programming in Java</title>
<author>Narayanan Jayaratchagan</author>
</article>
</journal>
<journal date="January 2004">
<article>
<title>Data Binding with XMLBeans</title>
<author>Daniel Steinberg</author>
</article>
</journal>
</catalog> -
Error coming
2007-01-09 15:06:15 javanew [Reply | View]
Thanks for the quick reply Deepak. I will try what you advised.
Actually, I was reading xml and xsd from file system and passing them as String. Now I am passing them as InputStream. Now I am getting these errors. Any idea?
cvc-elt.1: Cannot find the declaration of element 'catalog'.
cvc-elt.1: Cannot find the declaration of element 'journal'.
cvc-elt.1: Cannot find the declaration of element 'article'.
cvc-elt.1: Cannot find the declaration of element 'title'.
cvc-elt.1: Cannot find the declaration of element 'author'.
cvc-elt.1: Cannot find the declaration of element 'journal'.
cvc-elt.1: Cannot find the declaration of element 'article'.
cvc-elt.1: Cannot find the declaration of element 'title'.
cvc-elt.1: Cannot find the declaration of element 'author'.
XML Document has Error:true cvc-elt.1: Cannot find the declaration of element 'author'.
-
Error while validating with a DTD.
2006-08-03 13:51:13 Salli [Reply | View]
Hi,
I am trying to follow your code and validate my xml file against a DTD, specified externally (as my xml file does not come with a DOCTYPE element). Please see the code below.
It gives me the following error:
XML Document has Error:true cvc-elt.1: Cannot find the declaration of element 'SCustomerList'.
---------------------
public void validateSchema()
{
String schemaUrl = SCHEMA_URL;
String xmlDocumentUrl = XML_DOCUMENT_URL;
//check for validity with the Scims Xml output dtd
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
"file://c:/scims-data/sSchema.dtd");
//SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Document doc = null;
try{
DocumentBuilder parser = factory.newDocumentBuilder();
Validator handler=new Validator();
parser.setErrorHandler(handler);
doc = parser.parse("file:///c:/s-data/sxml.xml");
if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError+" "+handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");
}
catch (ParserConfigurationException e){
System.out.println("Parser not configured: " + e.getMessage());
}
catch (SAXException e){
System.out.print("Parsing XML failed due to a:\n" + e.getClass().getName()
+ "\n in Line number:" + ((SAXParseException) e).getLineNumber()
+ "\n Coloumn Number:" + ((SAXParseException) e).getColumnNumber()
+ "\n Xml schema file is:" + ((SAXParseException) e).getSystemId());
e.printStackTrace();
System.out.println("\n"+e.getMessage());
}
catch (IOException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
private class Validator extends DefaultHandler
{
public boolean validationError =false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException { validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}
public static void main(String artgs[])
{
DocumentBCValidator docValidator= new DocumentBCValidator();
docValidator.validateSchema();
}
-------------------
Your help would be appreciated.
Thanks -
Error while validating with a DTD.
2006-08-03 14:00:43 Salli [Reply | View]
Actually the error is this:
The markup in the document preceding the root element must be well-formed.
Earlier I had missed a backslash in specifying the source for the schema document and it was not able to find the DTD file. -
Error while validating with a DTD.
2006-08-05 13:00:10 Deepak Vohra [Reply | View]
The markup in the document preceding the root element must be well-formed.
Remove any spaces or empty lines before the prolog.
-
Handler exception
2006-06-14 02:35:57 nnc [Reply | View]
i am trying to use jdk1.4 SAXParserFactory and with setValidating(true) it doesnot recognize the attributes or xml tags.
When this code is run with Catalog.xml as argument, the error is "true Attribute "date" is not declared for element "journal"".
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class SchemaValidator1
{
public void validateSchema(String XmlDocumentUrl)
{
SAXParserFactory factory;
try{
Validator handler = new Validator();
factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(XmlDocumentUrl, handler);
if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError +" " + handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid"); }
catch(java.io.IOException ioe){
System.out.println("IOException"+ioe.getMessage());
}catch (SAXException se) {
System.out.println("SAXException"+se.getMessage()); }
catch(Exception e){
System.out.println("Exception"+e.getMessage()); }
}
private class Validator extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError=true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
validationError = false;
saxParseException=exception;
}
}
public static void main(String[] argv)
{
String XmlDocumentUrl=argv[0];
SchemaValidator1 validator=new SchemaValidator1();
validator.validateSchema(XmlDocumentUrl);
}
}
I would greatly appreciate if you can look into this .. -
Handler exception
2006-06-14 07:23:45 Deepak Vohra [Reply | View]
Please post the XML document and XML Schema. -
Handler exception
2006-06-14 21:44:49 nnc [Reply | View]
hi Deepak,
Many thanks for looking in to this. The xml and schema was donloaded from your site, here it is. infact it gives the same exception for all the elements in the xml file. (i tried removing the attribute)
<?xml version="1.0" encoding="UTF-8"?>
<!--A OnJava Journal Catalog-->
<catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"file://c:/Schemas/catalog.xsd"
title="OnJava.com" publisher="O'Reilly">
<journal date="April 2004">
<article>
<title>Declarative Programming in Java</title>
<author>Narayanan Jayaratchagan</author>
</article>
</journal>
</catalog>
schema,
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="title" type="xs:string"/>
<xs:attribute name="publisher" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="journal">
<xs:complexType>
<xs:sequence>
<xs:element ref="article" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="date" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="article">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author" type="xs:string"/>
</xs:schema>
-
Validating more than one schema
2006-05-21 08:51:36 iragoler [Reply | View]
Hi, very good article, thanks
I have an XML that has more than one schema (it is a SOAP message in my case). I want to validate all the schemas.
I tried to validate only one schema but it fails as the other schema doesn't match.
Here is the XML:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:m="http://www.example.org/timeouts"
xmlns:xml="http://www.w3.org/XML/1998/namespace">
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Sender</env:Value>
<env:Subcode>
<env:Value>m:MessageTimeout</env:Value>
</env:Subcode>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">Sender Timeout</env:Text>
</env:Reason>
<env:Detail>
<m:MaxTime>P5M</m:MaxTime>
</env:Detail>
</env:Fault>
</env:Body>
</env:Envelope>
When validating the SOAP schema, it fails because of the xml:lang.
How can I validate all the schemas?
Thanks
-
Validating more than one schema
2006-05-21 09:24:48 Deepak Vohra [Reply | View]
Also refer
http://www-128.ibm.com/developerworks/xml/library/x-tipsch.html -
Validating more than one schema
2006-05-21 09:21:07 Deepak Vohra [Reply | View]
Please refer
http://www-128.ibm.com/developerworks/xml/library/x-tipschnm.html
-
sample code quality - bad
2006-04-24 06:23:48 gninneh [Reply | View]
Sadly, you don't print the sample code in one piece, but with lots of comments in between here in the text. So no way to copy-paste-tryout.
If you tell me to use the downloadable code now, I am sorry that I have to say that, but this code is so badly formatted that I wonder how you can dare to publish this - it's really ugly.
As a result: maybe interesting article, but the code samples are next to unusable. -
sample code quality - bad
2006-04-24 07:09:04 Deepak Vohra [Reply | View]
The SchemaValidator.java and JAXPValidator.java do not have any comments. The applications have some System.out statements to output the validaity of an XML document.
Which code samples are you referrring to?
thanks,
Deepak -
sample code quality - bad
2006-04-26 03:26:17 gninneh [Reply | View]
I mean the comments in the text here _not_ the downloadable files. It would be better to print the whole class in one piece and add comments either in correct source code format in between, Ior before or after the classes' code. Then people could just copy and paste that code from the browser.
As opposed to that, the downloadable code has no comments and is probably runnable, but it's absolutely unreadable because it has an indentation that seems to be made by some randomuzer function - even the eclipse source formatter isn't able anymore to make something nice out of it.
Did you ever look at this downloadable code?
How do you think does it look? -
sample code quality - bad
2006-04-26 08:02:02 Deepak Vohra [Reply | View]
Please add indentation with Ctrl+Shift+F in Eclipse. -
sample code quality - bad
2006-05-26 06:45:06 gninneh [Reply | View]
Thanks for this very helpful hint. Did you try this yourself?
I cite from my previous post:
"Even the eclipse source formatter isn't able anymore to make something nice out of it."
You indent randomly, write multiple lines of code in one, completely at random. This is so bad that even eclipse can't make it better automatically.
Do you really think code should look like this? -
sample code quality - bad
2006-05-27 16:06:51 Deepak Vohra [Reply | View]
I have formatted the code.
When posted, the formatting does not get displayed.
-
sample code quality - bad
2006-05-27 16:01:56 Deepak Vohra [Reply | View]
Formatted code:
JAXPValidator.java:
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class JAXPValidator {
public void validateSchema(String SchemaUrl, String XmlDocumentUrl) {
try {
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
SchemaUrl);
DocumentBuilder builder = factory.newDocumentBuilder();
Validator handler = new Validator();
builder.setErrorHandler(handler);
builder.parse(XmlDocumentUrl);
if (handler.validationError == true) {
System.out.println("XML Document has Error:" +
handler.validationError + " " +
handler.saxParseException.getMessage());
} else {
System.out.println("XML Document is valid");
}
} catch (java.io.IOException ioe) {
System.out.println("IOException " + ioe.getMessage());
} catch (SAXException e) {
System.out.println("SAXException" + e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(
"ParserConfigurationException " +
e.getMessage());
}
}
public static void main(String[] argv) {
String SchemaUrl = argv[0];
String XmlDocumentUrl = argv[1];
JAXPValidator validator = new JAXPValidator();
validator.validateSchema(SchemaUrl, XmlDocumentUrl);
}
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception)
throws SAXException {
}
}
}
SchemaValidator.java:
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
public class SchemaValidator {
public void validateSchema(String SchemaUrl, String XmlDocumentUrl) {
SAXParser parser = new SAXParser();
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
SchemaUrl);
Validator handler = new Validator();
parser.setErrorHandler(handler);
parser.parse(XmlDocumentUrl);
if (handler.validationError == true) {
System.out.println("XML Document has Error:" +
handler.validationError + "" +
handler.saxParseException.getMessage());
} else {
System.out.println("XML Document is valid");
}
} catch (java.io.IOException ioe) {
System.out.println("IOException" + ioe.getMessage());
} catch (SAXException e) {
System.out.println("SAXException" + e.getMessage());
}
}
public static void main(String[] argv) {
String SchemaUrl = argv[0];
String XmlDocumentUrl = argv[1];
SchemaValidator validator = new SchemaValidator();
validator.validateSchema(SchemaUrl, XmlDocumentUrl);
}
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception)
throws SAXException {
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception)
throws SAXException {
}
}
}
-
Validation with javax.xml.validation package
2006-04-07 14:21:10 Deepak Vohra [Reply | View]
To validate with JDK 6.0 javax.xml.validation API.
1. Create a SchemaFactory instance.
SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
2. Create a Schema object from SchemaFactory.
Schema schema=factory.newSchema(schemaDocument);
3. Create a Validator object from Schema object.
Validator validator=schema.newValidator();
4. Create a class that implements the DefaultHandler.
private class ErrorHandlerImpl extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}
5. Create a ErrorHandler object and set the ErrorHandler on the Validator object.
ErrorHandlerImpl errorHandler=new ErrorHandlerImpl();
validator.setErrorHandler(errorHandler);
6. Create a Document object either by parsing an XML document. Create a DOMSource object from the Document object.
DOMSource source = new DOMSource(document);
7. Validate the DOMSource object with Validator object.
validator.validate(source););
8. If XML document has validation errror, the validation error is output by the ErrorHandler.
if(errorHandler.validationError==true)
System.out.println("XML Document has Error:"+errorHandler.validationError+" "+errorHandler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");}
-
Validation with javax.xml.validation package
2006-04-07 14:23:11 Deepak Vohra [Reply | View]
Correction.
6. Create a Document object either by parsing an XML document. Create a DOMSource object from the Document object.
Document document;
DOMSource source = new DOMSource(document);
7. Validate the DOMSource object with Validator object.
validator.validate(source);
-
Null Pointer exception while running
2005-12-18 20:53:08 NehaK [Reply | View]
Hi
I have already set these 2 jars(jaxp-api.jar and xercesImpl.jar) in the classpath. This is the code what u have given for validation with JAXP. I have modified only the main function where i am hardcoding the schema and document path. Could it be because of some problem with jdk vesion or some problem with the jdk installation? I have recently installed jdk frim from java.sun.com. follwoing are the details of the jsk version.
Windows Platform - J2SE(TM) Development Kit 5.0 Update 6
Windows Offline Installation, Multi-language jdk-1_5_0_06-windows-i586-p.exe 59.86 MB
Please help me in resolving the issue.
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.ErrorHandler;
public class JAXPValidator
{
public void validateSchema(String SchemaUrl, String XmlDocumentUrl)
{
try
{
System.out.println("
Inside JAXPValidator");
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
System.out.println("
After DocumentBuilderFactory");
factory.setNamespaceAware(true);
factory.setValidating(true);
System.out.println("
Values set After DocumentBuilderFactory");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema" );
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",SchemaUrl);
System.out.println("
Attributes set After DocumentBuilderFactory");
DocumentBuilder builder =factory.newDocumentBuilder();
System.out.println("
After DocumentBuilder");
Validator handler=new Validator();
builder.setErrorHandler(handler);
System.out.println("
After Validator");
builder.parse(XmlDocumentUrl);
System.out.println("
After builder.parse");
if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError+" "+handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");
}
catch(java.io.IOException ioe)
{
System.out.println("IOException "+ioe.getMessage());
}
catch (SAXException e)
{
System.out.println("SAXException"+e.getMessage());
}
catch (ParserConfigurationException e)
{
System.out.println("ParserConfigurationException " + e.getMessage());
}
}
private class Validator extends DefaultHandler
{
public boolean validationError =false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{ }
}
public static void main(String[] argv)
{
String SchemaUrl = "file:/C:/PServer/server/nodes/default/archives/public_html/reportinfra/WEB-INF/properties/ReportQueries.xsd";
String XmlDocumentUrl = "fike:/C:/PServer/server/nodes/default/archives/public_html/reportinfra/WEB-INF/properties/ReportSqlQueries.xml";
JAXPValidator validator=new JAXPValidator();
validator.validateSchema(SchemaUrl, XmlDocumentUrl);
}
}
Thanks & Regards
Neha
-
Null Pointer exception while running
2005-12-19 08:32:20 Deepak Vohra [Reply | View]
The application in the tutorial was developed with JDK 1.4.2. Please use JDK 1.4.2.
-
Null Pointer exception while running
2005-12-17 03:21:25 NehaK [Reply | View]
hi
While running this code i am getting the below mentionrd error. I am using the JAXP Parser. I have set the 2 jars in the classpath also. and i am using jdk1.5 version.
Kindly suggest what could be the issue.
Exception in thread "main" javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl could not be instantiated: java.lang.NullPointerException
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:104)
at JAXPValidator.validateSchema(JAXPValidator.java:19)
at JAXPValidator.main(JAXPValidator.java:82)
-
Null Pointer exception while running
2005-12-17 14:23:44 Deepak Vohra [Reply | View]
1. Add xercesImpl.jar to Classpath.
2. System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
-
Exceptions when run
2005-12-17 03:18:16 NehaK [Reply | View]
Hi
Your code is very helpful.
But while running this code i am getting the below mentioned error. I have set the 2 jars in classpath also. and i have installed jdk 1.5 version for this.
Please suggest what could be the issue.
Exception in thread "main" javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl could not be instantiated: java.lang.NullPointerException
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:104)
at JAXPValidator.validateSchema(JAXPValidator.java:19)
at JAXPValidator.main(JAXPValidator.java:82)
-
Exceptions when run
2005-12-17 03:18:10 NehaK [Reply | View]
Hi
Your code is very helpful.
But while running this code i am getting the below mentioned error. I have set the 2 jars in classpath also. and i have installed jdk 1.5 version for this.
Please suggest what could be the issue.
Exception in thread "main" javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl could not be instantiated: java.lang.NullPointerException
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:104)
at JAXPValidator.validateSchema(JAXPValidator.java:19)
at JAXPValidator.main(JAXPValidator.java:82)
-
Exceptions when run
2005-12-17 03:17:52 NehaK [Reply | View]
Hi
Your code is very helpful.
But while running this code i am getting the below mentioned error. I have set the 2 jars in classpath also. and i have installed jdk 1.5 version for this.
Please suggest what could be the issue.
Exception in thread "main" javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl could not be instantiated: java.lang.NullPointerException
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:104)
at JAXPValidator.validateSchema(JAXPValidator.java:19)
at JAXPValidator.main(JAXPValidator.java:82)
-
Validation does not work with Xerces or JAXP
2005-12-16 09:38:44 sathishforxml [Reply | View]
Following is the code:
JAXP
-----
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import java.io.FileReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
public class DocumentDriver
{
public static void main(String[] args) {
try{
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
"patient.xsd");
DocumentBuilder builder = factory.newDocumentBuilder();
PatientHandler handler = new PatientHandler();
builder.setErrorHandler(handler);
builder.parse("sms_xml_sls.xml");
handler.handlePatients();
} catch (Exception e){
System.out.println("Error while parsing" + e.getMessage());
}
}
}
Xerces
-------
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import java.io.FileReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.Properties;
import org.apache.xerces.parsers.SAXParser;
public class PatientDriverXerces
{
public static void main(String[] args) {
try{
org.apache.xerces.parsers.SAXParser parser = new org.apache.xerces.parsers.SAXParser();
InputSource source = new InputSource(new FileReader("sms_xml_sls.xml"));
PatientHandler handler = new PatientHandler();
parser.setContentHandler(handler);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","patient.xsd");
//parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setErrorHandler(handler);
parser.parse(source);
//parser.parse(source,(DefaultHandler)handler);
handler.handlePatients();
} catch (Exception e){
System.out.println("Error while parsing" + e.getMessage());
}
}
}
Am I missing anything?
-
Validation does not work with Xerces or JAXP
2005-12-16 10:51:15 Deepak Vohra [Reply | View]
What is the error message?
Is the schema and the XML document in the same directory?
Specifiy a file url for the schema and the XML document.
Is a Error handler defined?
-
Excellent Article
2005-11-17 17:18:06 girish_bhatia@hotmail.com [Reply | View]
Excellent article. worked fine. Thanks for providing good source code.
- Girish
-
Exceptions when run
2005-08-22 03:29:26 khylo [Reply | View]
HI,
I have copies the program verbatim, and when I try to run it I get the following exceptions.
I have added the xercesImpl.jar and xml-apis.jar to my classpath, but still no success.
Any idea?
Cheers,
Keith
Exception in thread "main" java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setAttribute(Ljava/lang/String;Ljava/lang/Object;)V
at com.aol.shopping.pinpoint.frontend.rearch.JAXPValidator.validateSchema(JAXPValidator.java:63)
at com.aol.shopping.pinpoint.frontend.rearch.JAXPValidator.validateSchema(JAXPValidator.java:52)
at com.aol.shopping.pinpoint.frontend.rearch.JAXPValidator.main(JAXPValidator.java:28)
-
schema check failed
2005-08-01 09:34:57 Coke [Reply | View]
Hi,
I tried both validation methods. But they have the same problem. They can only check the xml format, such as the tag is not matching. But when I change the schema, the validation can not find it. For example: in the catolog.xsd, I change the element "journal" to "journals", and validate the catolog.xml file. It passed.
Can someone help me with this problem?
Thanks in advance! -
schema check failed
2005-08-13 14:28:21 Deepak Vohra [Reply | View]
Tested with journals instead of journal.
A validation error gets generated.
,
XML Document has Error:truecvc-complex-type.2.4.a: Invalid content was found starting with element 'journal'. One of '{journals}' is expected.
-
Parsing XML Schema
2005-06-16 00:34:46 AbhijeetSun [Reply | View]
Sir ,
U have given a good validation process.
But i am facing problem in jdk1.4.
I want to know that it will work with jdk1.4 or jdk1.5?
Please suggest me.
thanks
abhijeet -
Parsing XML Schema
2005-06-16 05:07:02 Deepak Vohra [Reply | View]
The procedure applies to both jdk 1.4 and jdk 1.5.
What is the error message you are getting?
-
IOException
2005-03-25 06:14:52 Deepak Vohra [Reply | View]
If the url of type file://C:/ produces an IOException, specify the schema file/xml file url as
file:/C:/
for example,file:/C:/schemas/catalog.xsd
-
I only get Errors :-/
2005-03-25 04:47:37 Jonas123 [Reply | View]
Hi,
both examples doesn't work with Xerces 2.6.2 - maybe someone can help me?
I get this errors:
java.lang.NoClassDefFoundError: org/apache/xerces/parsers/AbstractSAXParser
or:
javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
Jonas123 -
I only get Errors :-/
2005-03-25 06:30:09 Deepak Vohra [Reply | View]
To validate with the Xerces 2.6.2 parser compile the SchemaValidator.java program.
To validate with the JAXP parser compile the JAXPValidator.java program.
Add the jar files specified in the Preliminary Setup section to the Classpath. -
I only get Errors :-/
2005-03-25 06:17:36 Deepak Vohra [Reply | View]
Tested the SchemaValidator.java program with the Xerces 2.6.2. The xml document gets validated with the schema.
Have you addedxerces-2_6_2/xercesImpl.jarandxml-apis.jarthe Classpath?
-
I only get Errors :-/
2005-03-25 10:27:31 Jonas123 [Reply | View]
Hi,
>Have you added xerces-2_6_2/xercesImpl.jar and xml-apis.jar the Classpath?
Maybe that is the problem. The missing class "org.apache.xerces.parsers.SAXParser" is inside xercesImpl.jar.
I use the document validation inside a plugin for a java program called "poseidon" (http://www.gentleware.com). The plugin is a .jar-file that is loaded by poseidon. The plugin jar-file uses some jar's from poseidon, they are inside poseidon program-directory/lib. I tried to copy xercesImpl.jar to poseidon lib-directory, I also tried to include it into my plugin jar-file - but that doesn't help. I am not sure if there is another classpath i have to use and where to find it (I use win XP and JRE 1.4.2).
Maybe you have an idea? -
I only get Errors :-/
2006-11-21 05:10:08 srivas2 [Reply | View]
I know that this happened a long time ago but could you tell me how you solved this problem? because i have a similar one using poseidon. -
I only get Errors :-/
2005-03-25 10:38:35 Deepak Vohra [Reply | View]
BTW, which plugin is used to run a java application? -
I only get Errors :-/
2005-03-25 10:48:58 Deepak Vohra [Reply | View]
Add the xerces jar files to the System CLASSPATH. -
Not able to get it to work :(
2005-04-20 00:19:36 java_kid [Reply | View]
Inspite of my following all the given steps,
it is trying to validate the file from a DTD and gives me the following :
Parsing error: Document root element "catalog", must match DOCTYPE root "null".
Parsing error: Document is invalid: no grammar found.
Parsing completed with errors
Any idea what the problem could be??
-
Not able to get it to work :(
2005-04-20 14:10:39 Deepak Vohra [Reply | View]
Set the schema validation and schema validation full-checking features.
parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
Because the schema validation is not set the parser parses with a DTD which may not be specified.
-
Not able to get it to work :(
2005-04-20 14:09:52 Deepak Vohra [Reply | View]
Set the schema validation and schema validation full-checking features.
[code]parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true); [/code]
Because the schema validation is not set the parser parses with a DTD which may not be specified.
-
Not able to get it to work :(
2005-04-20 04:44:13 Deepak Vohra [Reply | View]
For which parser do you get the errors?
Xerces or JAXP?
-
Better validation : xs:restriction & xs:pattern
2004-09-23 01:31:00 http://www.r0main.com [Reply | View]
This is mainly a structural view of the document...
For a deeper level of validation, one may use xs:restriction & xs:pattern to validate content of xml files ! -
Better validation : xs:restriction & xs:pattern
2004-09-23 06:03:59 Deepak Vohra [Reply | View]
The<xs:restriction/>element in a<xs:simpleType/>may be used to constrain the value space of data types.
<xs:restriction/>sub-element<xs:pattern/>is used to constrain the value space of data types to literals which match a regular expression.





I use Digester version 1.5 and xerces
i defined default in my xsd. when parsing the xml i don't get them.
Code:
digester.setFeature("http://xml.org/sax/features/validation",true);
digester.setFeature("http://apache.org/xml/features/validation/schema",true);
digester.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
digester.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","Statistics.xsd");
The feature ://digester.setFeature("http://apache.org/xml/features/validation/normalize-attribute-values",true); is not regognized by xerces.
What can i do?