• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in validating an in-memory xml against schemas

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Satyanarayan Pradhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Now when i copied the required schema to the folder from where i am executing the program, I am getting the following error for the method (in approach 2) as described earlier: Can anybody help me out where i am erring?

regards,
Satya
 
Satyanarayan Pradhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! I forgot to provide the stacktrace.
org.jdom.input.JDOMParseException: Error on line 1: TargetNamespace.1: Expecting namespace 'file:///C:/instdir/jdom-b10/test/ean-ucc/cip/CatalogueItemPublicationProxy.xsd', but the target namespace of the sch
ema document is 'http://www.ean-ucc.org/schemas/1.3.1/eanucc'.
at com.gxs.datasync.ccf.message.XMLMessageEngine.validateCIPMessage(XMLMessageEngine.java:265)
at com.gxs.datasync.ccf.message.XMLMessageEngine.validateMessageAgainstSchema(XMLMessageEngine.java:69)
at com.gxs.datasync.ccf.message.XMLMessageEngine.main(XMLMessageEngine.java:440)
Caused by: org.jdom.input.JDOMParseException: Error on line 1: TargetNamespace.1: Expecting namespace 'file:///C:/instdir/jdom-b10/test
/ean-ucc/cip/CatalogueItemPublicationProxy.xsd', but the target namespace of the schema document is 'http://www.ean-ucc.org/schemas/1.3
.1/eanucc'.
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:466)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:849)
at com.gxs.datasync.ccf.message.XMLMessageEngine.validateCIPMessage(XMLMessageEngine.java:258)
... 2 more
Caused by: org.xml.sax.SAXParseException: TargetNamespace.1: Expecting namespace 'file:///C:/instdir/jdom-b10/test/ean-ucc/cip/Catalogu
eItemPublicationProxy.xsd', but the target namespace of the schema document is 'http://www.ean-ucc.org/schemas/1.3.1/eanucc'.
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1171)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:455)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value for property http://apache.org/xml/properties/schema/external-schemaLocation
should be the form "[namespace] [whitespace] [schemafile]".
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic