XML Schema validation against multiple schemas using Xerces SAX
Joined: Mar 23, 2011
I am using Xerces-j 2.11.0 parser to validate an XML against Schema. The problem is the validating schema includes other types using xmlns: notation. These types are defined in other xsd files. When I run my validation, I get the following error:
Message: cvc-elt.1.a: Cannot find the declaration of element 'types:MyRoot'.
To fix this, I did some googling and added a ResourceResolver class as some of them suggested.
In the ResourceResolver class, I have overridden resolveResource method. I still get the same error. I dont even see that the resolveResource method is being called. I had put loggers inside the class. I thought just doing
is sufficient. Is there any wiring I am missing here. Any help or guidance in this regard will be appreciated.
NOTE: I am going for the SAX based validation because basically I need to parse the XML only once, strip off and add some stuff and validate the resultant XML. So I feel there is no need to go for DOM (using DocumentBuilderFactory stuff) for this. I might be wrong, but that's the stance I have taken. Experts, please advice.
Joined: Mar 23, 2011
Hi did some googling and study of the Xerces API and did some changes:
Commented out the p.parse API, as I understand this obviously may not do the validation, if setValidating(false). So replaced this with:
But on running this I get the following exception (the flow now goes inside the resolveResource overidden method):
[java] java.lang.NullPointerException [java] at sun.misc.MetaIndex.mayContain(MetaIndex.java:225)
[java] at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:747)
[java] at sun.misc.URLClassPath.getResource(URLClassPath.java:169)
[java] at sun.misc.URLClassPath.getResource(URLClassPath.java:221)
[java] at java.lang.ClassLoader.getBootstrapResource(ClassLoader.java:1150)
[java] at java.lang.ClassLoader.getResource(ClassLoader.java:999)
[java] at java.lang.ClassLoader.getResource(ClassLoader.java:997)
[java] at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1192)
[java] at java.lang.Class.getResourceAsStream(Class.java:2030)
[java] at ResourceResolver.resolveResource(Unknown Source)
[java] at org.apache.xerces.util.DOMEntityResolverWrapper.resolveEntity(Unknown Source)
[java] at org.apache.xerces.impl.XMLEntityManager.resolveEntity(Unknown Source)
This is because systemId is passed as null. I have no clue how to pass this systemId from XML. Even the publicId is null. However the nameSpaceURI shows up as:
Wonder if there is a way to make this work without using the systemId and publicId but using the nameSpaceURI, since that the only thing that gets passed to resolveResource other than the baseURI. Any help is appreciated.
You can create an InputSource from your File (by using the constructor which accepts an InputStream), and then you can call the InputSource's setSystemId() method. I don't know if this is the right thing to do but it isn't hard to try.