Validate an xml file using SAX Parser with included schema files.
843834Mar 18 2003 — edited Oct 1 2007Hi,
I try to validate an xml file using SAX Parser with schema. The program can recognize my first xsd file �mySchema.xsd� because I use a fully qualified path. However, when I run my program, it throws the following errors:
Failed to read included schema document SchemaA.xsd.
Failed to read included schema document SchemaB.xsd.
Failed to read included schema document SchemaC.xsd.
Because the �mySchema.xsd� includes another three schema files, the program couldn�t resolve the path for those schema files. If I put all schema files in the same directory as my xml file or use the fully qualified path for those three included schema files, it works fine but I can�t do that in the application. Is there any way to specify the path of those nested schema files in the Sax parser programmatically in the Java code?
Thanks in advance for help.
Tony
The following is the java code and xsd files.
/* Here is some constant used to set properties of the SAX parser. */
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String MY_SCHEMA_FILE = "mySchema.xsd";
/* Here is my parser method */
public void parser() {
DefaultHandler handler = new DefaultHandler ();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
String file = �C:/JavaTest/Schema/� + MY_SCHEMA_FILE;
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File(file));
File xmlFile = new File(�C:/JavaTest/Xml/myXml.xml�);
saxParser.parse(xmlFile, handler);
}
<------------- Here is MySchema.xsd -------------------------------------------->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="SchemaA.xsd"/>
<xs:include schemaLocation=" SchemaB.xsd"/>
<xs:include schemaLocation=" SchemaC.xsd"/>
</xs:schema>
<------------- Here is SchemaA.xsd -------------------------------------------->
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation=" SchemaD.xsd"/>
<xs:include schemaLocation=" SchemaE.xsd"/>
<xs:include schemaLocation=" SchemaF.xsd"/>
</xs:schema>