Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set schema at runtime

 
Caroline Iux
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Bill
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Caroline Iux
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?>
<Book>
<Author>OO</Author>
</Book>
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of no DTD or Schema I think you should try this:
<?xml version="1.0" standalone="yes" ?>
Bill
 
zhang qi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
newInstance().newDocumentBuilder();
doc_builder.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity (String publicId, String systemId)
{
try{
File f=new File(path);
final InputSource is=new InputSource(
new FileInputStream( f ));
return is;
}catch(Exception ex)
{
ex.printStackTrace();
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();
System.out.println(nl.getLength()+"|"+doc.getNodeType());
}
}

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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic