I am sending an xml packet to a webservice.The xml is converted to object by jaxb and I want to validate this against xsd.The xml packet has a tag called NSDG details.
<NSDGDetails>
<Keys>
<Key Type="sapconnector" />
</Keys>
<TargetDetails>
<Organisation>Gateway</Organisation>
</TargetDetails>
<NSDGValidation>
<Processed>no</Processed>
<Result>pass</Result>
</NSDGValidation>
<ChannelRouting>
<Channel>
<URI>e:channeluri</URI>
<Product>e:channelproduct</Product>
<Version>e:channelverion</Version>
</Channel>
<ID Type="">id</ID>
<Timestamp>2015-01-12T17:24:05.835+05:30</Timestamp>
</ChannelRouting>
</NSDGDetails>
Now I am expecting that when I pass something like "abcd" in NSDGDetails tag
<NSDGDetails>abcd<NSDGDetails>
then I should get an error while validating the request against the xsd.
Below is my code for schema validation
JAXBContext sdjaxbContext =
JAXBContext.newInstance(SubmitDocument.class);
JAXBContext NsdgMsgJaxbContext =
JAXBContext.newInstance(NSDGMessage.class);
SchemaFactory sf =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("xsd0.xsd"));
Marshaller marshaller = NsdgMsgJaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(schema);
ValidationEventCollector validationCollector = new
EsangamValidationEventHandler();
marshaller.setEventHandler(validationCollector);
marshaller.marshal(message, System.out); // where message is the object of NSDGMessage class
if(validationCollector.hasEvents())
{ throw some errror. I am expecting an error in case of <NSDGDetails>random value </NSDGetails>
}
But, I am not getting any error in this scenario. The xsd file and the NSDGMessage class generated by jaxb can be found here xsd NSDGMessage
One thing I tried is to convert the message object to string and found that
<NSDGDetails>abcd<NSDGDetails> is changed to <NSDGDetails><NSDGDetails>I believe this is why the validator is not able to catch the error because the invalid data is removed.
Helpp...