• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set schema at runtime

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic