1

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...

1

1 Answer 1

0

When you go from XML to Java objects you need to use the Unmarshaller instead.

For a simple example see similar question in How to validate an XML against schema using JAXB?

2
  • Hi Escay, the link provided in the answer says we can enable validation while doing both marshaling and unmarshalling.I chose marshaling process since I am already having a java object but it is not working. I have also tried the unmarshalling process but I noticed when I convert my java object to XML string the invalid content disappears and the tag is left empty eg. <NSDGDetails>abcd<NSDGDetails> is changed to <NSDGDetails></NSDGDetails>. Now since I have allowed empty tags this is not getting caught in validation. Is there any solution for this?
    – Asim
    Aug 18, 2017 at 5:04
  • Yes, but your question starts with going from XML to Java object, which requires unmarshalling. Your code example for schema validation goes from Java object to XML (marshalling), which is incorrect based on the start of the question. The xsd provided in your question is way too long to get to the point. In general the question is a duplicate of the one I mentioned. There are lots of examples available, reproduce the examples and replace their xsd with your xsd and go forward.
    – Escay
    Aug 18, 2017 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.