Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Schema validation using JAXP

Thread view: 
steve_marjoribanks@hotmail.com - 20 Feb 2006 11:59 GMT
I am currently trying to parse and validate and XML file against a W3C
schema using JAXP (SAX). I have found lots of examples using code
similar to:

SchemaFactory schemaFactory
=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaXSD = schemaFactory.newSchema(schema);
Validator validator = schemaXSD.newValidator();
validator.validate(schema);

As far as I can understand, this method uses a given schema file
('schema' in the code above) to validate the XML file against, and
usually this file would be specified at the command line or in the
application somehow.
However, I wish to *always* validate the XML file against the schema
given in the 'xs:schemaLocation' attribute within the XML file, *not*
against a schema file given by the user. How can I go about doing this?

Thanks

Steve
steve_marjoribanks@hotmail.com - 20 Feb 2006 16:25 GMT
Sorted it now:

// Create JAXP SAX parser factory...
            SAXParserFactory factory = SAXParserFactory.newInstance();
            // Set parsing properties...
            factory.setNamespaceAware(awareness);
            factory.setValidating(validating);
            // Create parser...
            SAXParser parser = factory.newSAXParser();
            // Enable schemas...
            parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            // Create reader...
            XMLReader xmlReader = parser.getXMLReader();
            // Create and assign content handler and error handler...
            SAXHandler handler = new SAXHandler();
            XMLErrorHandler errorHandler = new XMLErrorHandler();
            xmlReader.setContentHandler(handler);
            xmlReader.setErrorHandler(errorHandler);
            // Set file to be parsed...
            InputSource inputSource = new
InputSource(Application.filename.toURI().toString());
            inputSource.setSystemId(Application.filename.toURI().toString());
            // Parse the file...
            xmlReader.parse(inputSource);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.