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
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
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( XMLConstan ts.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.newInstan ce();
saxFactory.setValidating(t rue);
saxFactory.setNamespaceAwa re(true);
saxFactory.setSchema(schem a);
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.
I have created a schema object for url - "http://www.w3.org/2003/05/soap-envelope". as below
SchemaFactory scFactory = SchemaFactory.newInstance(
scFactory.setErrorHandler(
URL url = new URL("http://www.w3.org/2003/05/soap-envelope");
Schema schema = scFactory.newSchema(url);
SAXParserFactory saxFactory = SAXParserFactory.newInstan
saxFactory.setValidating(t
saxFactory.setNamespaceAwa
saxFactory.setSchema(schem
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.
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.
> URL url = new URL("http://www.w3.org/2003/05/soap-envelope");
points to a valid schema file.
ASKER
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.
I have tried using url.openStream() and given it to the SchemaFactory, then also it did'nt work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?