Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB 2.0 Validation not working
    text
    copied!<p>i am working on JAXB 2.0 and currently struck on the validation part as its not working as expected. below is the validation code</p> <pre><code>public void validateXMLToSchema(Unmarshaller ummarshaller,String xsdFileName) throws SAXException, JAXBException{ System.out.println(getClass().getResource(DEFAULT_XSD_NAME).toString()); Schema schema; SchemaFactory schemaFactory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); if(null==xsdFileName) schema=schemaFactory.newSchema(getClass().getResource(DEFAULT_XSD_NAME)); else schema=schemaFactory.newSchema(new File(xsdFileName)); ummarshaller.setSchema(schema); ummarshaller.setEventHandler( new ValidationEventHandler() { @Override public boolean handleEvent(ValidationEvent validationevent) { if(validationevent.getSeverity()==ValidationEvent.FATAL_ERROR || validationevent.getSeverity()==ValidationEvent.ERROR || validationevent.getSeverity()==ValidationEvent.WARNING){ ValidationEventLocator locator = validationevent.getLocator(); log.info("Line:Col[" + locator.getLineNumber() + ":" + locator.getColumnNumber() + "]:" + validationevent.getMessage()); } return true; } }); } </code></pre> <p>and here is the call to the method</p> <pre><code>Destination destination=new Destination(); try { destination=(Destination)unmarshal(Destination.class,new FileInputStream(new File("C:/Users/Raisonne/Desktop/jaxb/jaxb-ri-20101119/bin/destination.xml"))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } System.out.println(destination.getName()); } public static &lt;T&gt; T unmarshal( Class&lt;T&gt; docClass, InputStream inputStream ) throws JAXBException, SAXException { String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance( packageName ); Unmarshaller u = jc.createUnmarshaller(); XMLValidator xmlValidator=new XMLValidator(); xmlValidator.validateXMLToSchema(u, null); </code></pre> <p>i have few fields as required fields as per XSD but even removing them it shd give me the error while its not giving anything and parsing my xml file in to the corresponding object can any one point whats going wrong?</p> <p>here is the part of XSD</p> <pre><code> &lt;xs:element name="destination" type="Destination"/&gt; &lt;xs:complexType name="Destination"&gt; &lt;xs:sequence&gt; &lt;xs:element name="name" type="xs:string"/&gt; &lt;xs:element name="destinationID" type="xs:string" minOccurs="0"/&gt; &lt;xs:element name="shortDescription" type="xs:string" minOccurs="0"/&gt; &lt;xs:element name="longDescription" type="xs:string" minOccurs="0"/&gt; &lt;xs:element name="stateID" type="xs:string"/&gt; &lt;xs:element name="typeCode" type="xs:int"/&gt; &lt;xs:element name="countryCode" type="xs:string"/&gt; &lt;xs:element name="categories" type="xs:string"/&gt; &lt;xs:element name="transport" type="Transport" minOccurs="0" maxOccurs="1"/&gt; &lt;xs:element name="culture" type="Culture" minOccurs="0" maxOccurs="1"/&gt; &lt;xs:element name="events" type="Events" minOccurs="0" maxOccurs="1"/&gt; &lt;xs:element name="placesToVisit" type="PlacesToVisit" minOccurs="0" maxOccurs="1"/&gt; &lt;xs:element name="contacts" type="Contact" minOccurs="0" maxOccurs="1"/&gt; &lt;xs:element name="addresses" type="address" minOccurs="0" maxOccurs="1"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>and the Java file generated</p> <pre><code>@XmlElement(required = true) protected String name; protected String destinationID; protected String shortDescription; protected String longDescription; @XmlElement(required = true) protected String stateID; </code></pre> <p>i was removing stateID from the xml file but still no alarm in validation part</p> <p>thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload