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
Joined: Mar 22, 2000
Posts: 12269
1
posted
0
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
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
posted
0
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
Joined: Mar 22, 2000
Posts: 12269
1
posted
0
In the case of no DTD or Schema I think you should try this: <?xml version="1.0" standalone="yes" ?> Bill
zhang qi
Greenhorn
Joined: Nov 13, 2003
Posts: 1
posted
0
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()); } }