Ivan- It looks like you're right that the schema isn't being read. The way I made sure my schemaLocation attribute was set correctly in my XML file was by printing out the contents of the "user.dir" system property and setting the schemaLocation relative to that.
Hope that helps. Jeff Orford -----Original Message----- From: O'Connor, Ivan [mailto:ivan.o'[EMAIL PROTECTED] Sent: Wednesday, April 25, 2001 8:11 AM To: '[EMAIL PROTECTED]' Subject: How to do XML Schema validation using Xerces-J? I'm having a problem with XML Schema validation - it's as if the schema isn't being read. I'm parsing personal-schema.xml from the Xerces-J 1.3.0 distribution, and I've put together a simple program to parse the document: import java.io.IOException; import org.xml.sax.Attributes; import org.xml.sax.ErrorHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class Parser extends DefaultHandler { private XMLReader m_reader; public Parser() throws SAXException { // create the parser (controlled by the org.xml.sax.driver property) // pick up the Apache Xerces parser by default if (System.getProperty("org.xml.sax.driver") == null) { System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser"); } m_reader = XMLReaderFactory.createXMLReader(); // register XMLParser as the handler with the SAX parser m_reader.setErrorHandler(this); // switch on namespaces... m_reader.setFeature("http://xml.org/sax/features/namespaces", true); // and validation m_reader.setFeature("http://xml.org/sax/features/validation", true); } public void parse(String fileName) throws SAXException, IOException { // parse the document m_reader.parse(fileName); } // ErrorHandler interface public void warning(SAXParseException ex) { System.out.println("Warning: " + ex); } public void error(SAXParseException ex) { System.out.println("Error: " + ex); } public void fatalError(SAXParseException ex) { System.out.println("Fatal error: " + ex); } // Test code public static class Test { public static void main(String[] args) { if (args.length < 1) { System.err.println ("Usage: Parser$Test <xmlFile>"); System.exit(1); } else { try { Parser parser = new Parser(); parser.parse(args[0]); System.out.println("Finished parsing"); } catch (SAXException ex) { ex.printStackTrace(System.err); } catch(IOException io) { io.printStackTrace(System.err); } System.exit(0); } } } } When I run this code, passing personal-schema.xml as the file to parse, I get the following output: Error: org.xml.sax.SAXParseException: Element type "personnel" must be declared. Error: org.xml.sax.SAXParseException: Attribute "xsi:noNamespaceSchemaLocation" must be declared for element type "personnel". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "subordinates" must be declared for element type "link". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "manager" must be declared for element type "link". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "manager" must be declared for element type "link". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "manager" must be declared for element type "link". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "manager" must be declared for element type "link". Error: org.xml.sax.SAXParseException: Element type "person" must be declared. Error: org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "person". Error: org.xml.sax.SAXParseException: Element type "name" must be declared. Error: org.xml.sax.SAXParseException: Element type "family" must be declared. Error: org.xml.sax.SAXParseException: Element type "given" must be declared. Error: org.xml.sax.SAXParseException: Element type "email" must be declared. Error: org.xml.sax.SAXParseException: Element type "link" must be declared. Error: org.xml.sax.SAXParseException: Attribute "manager" must be declared for element type "link". Finished parsing Does anyone have any suggestions how I can fix this to get the parser to recognise the schema? By the way, the schema, xml instance and class are all living in the same directory. Thanks. Ivan This message is for the named person's use only. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. CREDIT SUISSE GROUP and each of its subsidiaries each reserve the right to monitor all e-mail communications through its networks. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of any such entity. Unless otherwise stated, any pricing information given in this message is indicative only, is subject to change and does not constitute an offer to deal at any price quoted. Any reference to the terms of executed transactions should be treated as preliminary only and subject to our formal written confirmation. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
