Avatar of pavanspecial
pavanspecial asked on

validate xml file against a specific schema using jaxp sax parser


I want to validate a soap xml given below
<?xml version="1.0" encoding="UTF-8"?>

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.

T.Pavan kumar

Avatar of undefined
Last Comment

8/22/2022 - Mon

>  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?

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();
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

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.


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.