| Author |
Need help in validating an in-memory xml against schemas
|
Satyanarayan Pradhan
Greenhorn
Joined: Jan 13, 2004
Posts: 3
|
|
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
Joined: Jan 13, 2004
Posts: 3
|
|
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
Joined: Jan 13, 2004
Posts: 3
|
|
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)
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
The value for property http://apache.org/xml/properties/schema/external-schemaLocation should be the form "[namespace] [whitespace] [schemafile]".
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
 |
|
|
subject: Need help in validating an in-memory xml against schemas
|
|
|