Thanks both of you for your answers.

Mukul's solution didn't work for me, I kept getting "premature end of file"
errors.
Michael's solution worked well :

Here are the detailed steps  :

1) Create a standard, namespace aware but non-validating XMLReader.


SAXParserFactory spf = SAXParserFactory.newInstance();

spf.setValidating(false);
spf.setNamespaceAware(true);

XMLReader reader = spf.newSAXParser().getXMLReader();


2) Create a sax filter that cheats the namespace declaration of the document
root element (<items> in my case)


XMLReader filterDecoratedReader = new SaxNamespaceCorrectionFilter(reader);

The filter code :

public class SaxNamespaceCorrectionFilter extends XMLFilterImpl
{


    public SaxNamespaceCorrectionFilter(XMLReader reader)
    {
        super(reader);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
    {

        if (localName.equals("items"))
        {
            //inject namespace uri...
            super.startElement("http://xml.import.com/schema/ItemsSchema";,
localName, qName, attributes);
        } else
        {
            //do things normally...
            super.startElement(uri, localName, qName, attributes);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
throws SAXException
    {
        if (localName.equals("items"))
        {
            //inject namespace uri...
            super.endElement("http://xml.import.com/schema/ItemsSchema";,
localName, qName);
        } else
        {
            //do things normally...
            super.endElement(uri, localName, qName);
        }
    }

} 


3) Create a SAX source on top of that :


SAXSource saxSource = new SAXSource(filterDecoratedReader, new
InputSource("I:\\TEMP\\Xml\\Itemsfo.xml"));

4) Load your schema file

String schemaLang = "http://www.w3.org/2001/XMLSchema";;
            SchemaFactory sf = SchemaFactory.newInstance(schemaLang);
            Schema schema =
sf.newSchema(Thread.currentThread().getContextClassLoader().getResource("javaapplication15/ItemsSchema.xsd"));


5) Extract a validator from your schema, sets the desired validation error
handler if necessary and call validate() passing a SAXResult object that
wraps your SAX event handler.


Validator validator = schema.newValidator();
validator.setErrorHandler( myErrorHandler);

validator.validate(saxSource, new SAXResult(  myDesiredContentHandler ) );

Kind regards
-- 
View this message in context: 
http://old.nabble.com/Parsing-and-validating-an-XML-Document-that-obeys-a-schema-but-doesn%27t-declare-it.-tp27942668p27950986.html
Sent from the Xerces - J - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-users-h...@xerces.apache.org

Reply via email to