My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Tuesday, July 16, 2013

Find errors in XML tags before unmarshalling






1.Create a JAXB2ValidationEventCollector-- File Atached herewith. Idealy you can put this in the same folder as that of your JAXB classes.
 2.Attach this Validator to the Unmarshaler.
 3.Now if you see any errors in the XSD with that of the field you are getting in as an XML, the Validator will spill out the same.
 4.If not assume that the XML is perfect to that of the XSD.
 5.I have tested this by manually changing the XSD.
 6.My XSD has the following
 





















1.My XML has the following EPH1035303164649
 2.I get the following 
 
msg-->cvc-length-valid: Value 'EPH1035303164649' with length = '16' is not facet-valid with respect to length '10' for type '#AnonType_ReferenceNumberOpfTriggerMessage'.

msg-->cvc-type.3.1.3: The value 'EPH1035303164649' of element 'xs:ReferenceNumber' is not valid.




XML message



   


 


 


 


  EPH1035303164649 


  


  


  




XSD











  


   


    


     


    


   


  




  


   


    


    


   


  




  


   


    


    


    


     


      


       


      


     


    


    


    


   


  


 


  


   


    


     


      


       


      


     


    


   


  





 




 Validator


import javax.xml.bind.ValidationEvent;



import javax.xml.bind.util.ValidationEventCollector;


public class JAXB2ValidationEventCollector extends ValidationEventCollector {


    @Override


    public boolean handleEvent(ValidationEvent event) {


        super.handleEvent(event);


        return true;


    }


}




Test File


import java.io.File;





import javax.xml.XMLConstants;


import javax.xml.bind.JAXBContext;


import javax.xml.bind.Unmarshaller;


import javax.xml.bind.ValidationEvent;


import javax.xml.bind.util.ValidationEventCollector;


import javax.xml.validation.Schema;


import javax.xml.validation.SchemaFactory;




import com.test.trigger.GoldenCopyTrigger;


import com.test.trigger.JAXB2ValidationEventCollector;




public class CheckXMLAgainstXSD {




  public static void main(String[] args){


   CheckXMLAgainstXSD classObj = new CheckXMLAgainstXSD();


   try {


    classObj.checkXML();


   } catch (Exception e) {


    // TODO Auto-generated catch block


    e.printStackTrace();


   }


  }


 


  private void checkXML() throws Exception{


        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 


         Schema schema = sf.newSchema(new File("C:/Data//jaxb/MilestoneTrigger.xsd")); 




         JAXBContext jc = JAXBContext.newInstance(GoldenCopyTrigger.class);




         Unmarshaller unmarshaller = jc.createUnmarshaller();


         unmarshaller.setSchema(schema);


         ValidationEventCollector validationCollector = new JAXB2ValidationEventCollector();


         unmarshaller.setEventHandler(validationCollector);




         GoldenCopyTrigger customer = (GoldenCopyTrigger) unmarshaller.unmarshal(new File("C:/Data/jaxb/sample.xml"));




         if(validationCollector.hasEvents())


         {


             for(ValidationEvent event:validationCollector.getEvents())


             {


                 String msg = event.getMessage();


                 System.out.println("msg-->"+msg);


             }


         } 


 


  }


}