Link to home
Start Free TrialLog in
Avatar of pavanspecial
pavanspecial

asked on

validate xml file against a specific schema using jaxp sax parser

Hi,

I want to validate a soap xml given below
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<name>pavan</name>
<number>123</number>
</Body>
</Envelope>

to validate against the schema of soap from url "http://www.w3.org/2003/05/soap-envelope". I dont want to have this namespace in the xml. How can I validate against the specified schema using jaxp sax parser?
Please help.

Thanks,
T.Pavan kumar
Avatar of girionis
girionis
Flag of Greece image

>  I dont want to have this namespace in the xml.

I am not sure I understand here. If you don't declare the namespace how will the parser know where that the Envelope elements belongs to the soap-envelope namespace?
Avatar of pavanspecial
pavanspecial

ASKER

I will be excepting only a soap xml from the client at he server.
I have created a schema object for url - "http://www.w3.org/2003/05/soap-envelope". as below

SchemaFactory scFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
scFactory.setErrorHandler(new DefaultErrorHandler());
URL url = new URL("http://www.w3.org/2003/05/soap-envelope");
Schema schema = scFactory.newSchema(url);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setValidating(true);
saxFactory.setNamespaceAware(true);
saxFactory.setSchema(schema);
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.parse(new InputSource(new FileReader("test.xml")));

Could'nt i give the xml given in the question as the test.xml to the parser. Its throwing the exception:
Cannot find the declaration of element 'Envelope'.
It has got the schema object in memory which would be having Envelope element. Is it not validating against the schema which i have set to the SAXFactory? Why do we require to give the namespace uri in the input xml to the parser?
Please help.
> Cannot find the declaration of element 'Envelope'.

It cannot find it because it does not know where to find it. You do not associate the Envelope element in the XML with the schema. You need to define the namespace in your XML and say that the Envelope belongs to that namespace.
And also make sure that this:

> URL url = new URL("http://www.w3.org/2003/05/soap-envelope");

points to a valid schema file.
ya its the url for the schema file of soap messages , "http://www.w3.org/2003/05/soap-envelope". And I'm sure that its a valid url for schema file.
I have tried using url.openStream() and given it to the SchemaFactory, then also it did'nt work.

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial