wood burning stoves 2.0
The moose likes XML and Related Technologies and the fly likes Set schema at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Set schema at runtime" Watch "Set schema at runtime" New topic

Set schema at runtime

Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
Is there a way to set schema to the parser at runtime? The reason being the xml file sent in is not guranteed to have schema included. In order to enforce validation, I'd like to tell the parser always use THE schema to validate. By the way, I am using xerces's parser.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
I have not tried this, but it seems to me that if you implement the EntityResolver interface you can run in custom entities on the fly.
Take a look at the org.xml.sax.EntityResolver interface in the JAXP documentation for an example.
Let me know what happens...
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782

Yes. Using EntityResolver, you can not only apply a Schema/DTD at runtime, you can also
choose from a set of available Schema/DTD and conditionally validate the same document
against a single XML.

Hope that helps!
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
It worked. I registered my schema so that the parser always use that. Here is my code:
parser.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// use the default behaviour
return new InputSource( schemaURL );
I have another problem though. If my xml file doesn't include any schema reference, it is just a well formed xml. It looks like the parser can't find the root element.
The error message is(assume book is the root element):
org.xml.sax.SAXParseException: Element type "Book" must be declared.
my xml file is like this:
<?xml version="1.0"?>
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
In the case of no DTD or Schema I think you should try this:
<?xml version="1.0" standalone="yes" ?>
zhang qi

Joined: Nov 13, 2003
Posts: 1
Could some one review this code, and tell me why it cannot run?

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class XMLTest
private static final String path="htmlent.dtd";
public static void main(String[] args)throws Exception
DocumentBuilder doc_builder = DocumentBuilderFactory.
doc_builder.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity (String publicId, String systemId)
File f=new File(path);
final InputSource is=new InputSource(
new FileInputStream( f ));
return is;
}catch(Exception ex)
return null;
String xmlString="<a>xyz   <b>bcd</b>ddfad</a>";
Document doc=doc_builder.parse(new InputSource(new StringReader(xmlString)));
NodeList nl=doc.getChildNodes();

The htmlent.dtd:
<!ENTITY nbsp CDATA " " -- no-break space = non-breaking space,
U+00A0 ISOnum -->
<!ENTITY iexcl CDATA "¡" -- inverted exclamation mark, U+00A1 ISOnum -->

Thanks for any reply!
I agree. Here's the link: http://aspose.com/file-tools
subject: Set schema at runtime
It's not a secret anymore!