views:

974

answers:

3

how to reference XSD Schema location while parsing XML via SAX Xerces?

< ?xml version="1.0" encoding="ISO-8859-1"?> < com.firma xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

>  
< !--  xsi:noNamespaceSchemaLocation="F:\...\myschema_v2.5.xsd"

Must I reference this element really within the XML Doc??? I hope, not... -- >

I also set it as follows in Java code, which is not elegant, while schema location is fixed(not appropriate for production)
SaxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", "F:...\myschema_v2.5.xsd" );

+1  A: 

include the schema in your jar and load it using getResourceAsStream in the following way

reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 
  new InputSource(getClass().getResourceAsStream(xsdLocation)));
Salandur
A: 

thx Salandur. but still getting the exception of:
SAX Error in parsing file, Cause: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'null', because

1) could not find the document;
2) the document could not be read;
3) the root element of the document is not .

A: 

I got it.

one must use as follows, giving "/com/firma/project/.../myschema_v2.5.xsd" as parameter. not forgetting the "/" in the path at the very beginning.