axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christer Holmér <Christer.Hol...@corustechnologies.com>
Subject RE: XML validation using schema. May be off topic with SOAP
Date Thu, 16 Jan 2003 10:01:51 GMT
You need to set the EntityResolver of your parser.
 
See http://xml.apache.org/xerces-j/apiDocs/org/xml/sax/EntityResolver.html
 
For example:
 
org.xml.sax.EntityResolver myResolver = new MyResolver();
String namespaceURI = "http://www.comp.com/mynamespace";    // The namespace
identifier
String schema;
// Get the schema definition from the DB or let the resolver itself do it
myResolver.addMapping(namespaceURI, schema);
parser.setEntityResolver(myResolver);
 
Hope this helps!
 
/Christer

-----Original Message-----
From: Praveen Peddi [mailto:ppeddi@contextmedia.com]
Sent: den 15 januari 2003 21:26
To: axis-user@xml.apache.org
Subject: XML validation using schema. May be off topic with SOAP


I want to validate an xml document with an existing schema. I know there is a
code to do it when the schema is present as a URL. BUt in my case schema is
present with in the application (since it is stored in the database) and that
is not the url.
I have the following code:
public static void DOMValidate(String XMLfile, String Schema){
  long startTime = System.currentTimeMillis();        
 
        // Instantiate the DOM parser.
        DOMParser parser = new DOMParser();
 
        // set the features
        try{
            parser.setFeature(" http://xml.org/sax/features/namespaces",true
<http://xml.org/sax/features/namespaces> );
            parser.setFeature(" http://xml.org/sax/features/validation",
true);
            parser.setFeature("
http://apache.org/xml/features/validation/schema",true
<http://apache.org/xml/features/validation/schema> );
            parser.setFeature("
http://apache.org/xml/features/validation/schema-full-checking",true
<http://apache.org/xml/features/validation/schema-full-checking> );
            // convert the xml file name to a URL
            String SystemId = null;
            try{
                SystemId = new File(Schema).toURL().toExternalForm();
            } catch(java.net.MalformedURLException ex){
                //ex.printStackTrace();
                log.error(ex);
            }
            parser.setProperty("
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",S
ystemId
<http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation>
);
            // myErrorHandler is a descendant of ErrorHandler, it should be
set here to be able to catch parse errors
            parser.setErrorHandler(new SchemaErrorHandler());
 
        } catch (SAXNotSupportedException ex){
            System.out.println("SAXNotSupportedException Exception");
        } catch (SAXNotRecognizedException ex){
                System.out.println("SAXNotRecognizedException Exception");
        }
 
        // parse the xml file, the errorhandler class has callbacks,
        // so those will be called automatically there is a parse error
        try{
                parser.parse(new File(XMLfile).toURL().toExternalForm());
                //System.out.println("Parsed Successfully by DOM Parser");
        } catch (org.xml.sax.SAXException ex){
                //System.out.println("SAXException Exception");
                //ex.printStackTrace();
            log.error(ex);
        } catch (java.io.IOException ex){
                System.out.println("IOException Exception");
   } finally {
   long endTime = System.currentTimeMillis();
         //System.out.println("Total time of
DOMValidate:"+(endTime-startTime));
 
 }
 
    }
 
How can I do the validation if the Schema is the actual schema string not the
url. This looks like a common problem. I assume that its not universal that
schema is not always stored externally.
 
Any suggestions are appreciated.
 
Praveen


Mime
View raw message