Hi,
I am facing problem in validating an inmemory xml
string against XSDs (with targetNamespace), need your help on this. Where as I am able to validate an XML file (existing in file system) against the required schemas, i am not able to do so using a String representation of the same XML. I am providing the both versions of my code below. Any suggestion regarding this would be greatly appreciated.
Note: The schams against which i am validating the XML are having targetNamespace defined.
Approach 1: (This works fine) Using Fully qualified XML file name for validation
boolean validateCIPMessage(String pXMLMessageFile, String pSchemaDir){
boolean valid = false;
try{
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
builder.setFeature("http://apache.org/xml/features/validation/schema", true);
builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
"file:///" + pSchemaDir + "/CatalogueItemPublicationProxy.xsd");
/*+ " " + "file:///" + pSchemaDir + "/As2Envelope.xsd"
+ " " + "file:///" + pSchemaDir + "/Identification.xsd"
+ " " + "file:///" + pSchemaDir + "/ExtendedTypes.xsd"
+ " " + "file:///" + pSchemaDir + "/Transaction.xsd"
+ " " + "file:///" + pSchemaDir + "/DocumentCommand.xsd"
+ " " + "file:///" + pSchemaDir + "/Command.xsd"
+ " " + "file:///" + pSchemaDir + "/Components.xsd"
+ " " + "file:///" + pSchemaDir + "/CatalogueItemPublication.xsd"
+ " " + "file:///" + pSchemaDir + "/CatalogueItemComponents.xsd");*/
builder.build(pXMLMessageFile); //Fully qualified XML file name as argument
valid = true;
}catch(IOException ioEx){
ioEx.printStackTrace();
}catch(JDOMException jdomEx){
jdomEx.printStackTrace();
}
return valid;
}
-----------------------------------
Approach 2: (This does not work) Using XML as a String object for validation
boolean validateCIPMessage(String pXMLMessage, String pSchemaDir){
boolean valid = false;
StringReader stringReader = null;
try{
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
builder.setFeature("http://apache.org/xml/features/validation/schema", true);
builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
"file:///" + pSchemaDir + "/CatalogueItemPublicationProxy.xsd");
/*+ " " + "file:///" + pSchemaDir + "/As2Envelope.xsd"
+ " " + "file:///" + pSchemaDir + "/Identification.xsd"
+ " " + "file:///" + pSchemaDir + "/ExtendedTypes.xsd"
+ " " + "file:///" + pSchemaDir + "/Transaction.xsd"
+ " " + "file:///" + pSchemaDir + "/DocumentCommand.xsd"
+ " " + "file:///" + pSchemaDir + "/Command.xsd"
+ " " + "file:///" + pSchemaDir + "/Components.xsd"
+ " " + "file:///" + pSchemaDir + "/CatalogueItemPublication.xsd"
+ " " + "file:///" + pSchemaDir + "/CatalogueItemComponents.xsd");*/
stringReader = new StringReader(pXMLMessage);
builder.build(stringReader);
valid = true;
}catch(IOException ioEx){
ioEx.printStackTrace();
}catch(JDOMException jdomEx){
jdomEx.printStackTrace();
}finally{
stringReader.close();
}
return valid;
}
On execution, this one throws the following error:
org.jdom.input.JDOMParseException: Error on line 1: schema_reference.4: Failed to read schema document 'CatalogueItemPublicationProxy.xsd', because 1) could not find the document; 2) the document could not be
read; 3) the root element of the document is not <xsd:schema>.
at com.gxs.datasync.ccf.message.XMLMessageEngine.validateCIPMessage(XMLMessageEngine.java:291)
Thanks in advance!
Best regards,
Satya