Note that there are some explanatory texts on larger screens.

plurals
  1. POWant to get all Errors List when I validate XML using JAXB
    text
    copied!<p>Can you please help me to get the list of all errors when I validate the XML file using JAXB?</p> <p>Right now I have already implemented the code for the same. I have implemented the code using unmarshalling the XML file. </p> <p>When I unmarshalling the XML file at that time if any error would be there in the XML file, I got the exception but it came out after giving the first exception but I want the list of all errors of that XML file. This should not come out after getting the first error.</p> <pre><code>Source xsdFileName = new StreamSource(new File("validate6.xsd")); Source xmlFileName = new StreamSource(new File("test3.xml")); try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(xsdFileName); JAXBContext jc = JAXBContext.newInstance(Innovations.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new MyValidationEventHandler()); Innovations innovation = (Innovations) unmarshaller.unmarshal(xmlFileName); System.out.println("Total Size :: " + innovation.getInnovationList().size()); System.out.println(xmlFileName.getSystemId() + " is valid"); } catch (JAXBException e) { e.printStackTrace(); System.out.println(xmlFileName.getSystemId() + " is NOT valid"); } catch (SAXParseException e) { e.printStackTrace(); System.out.println(xmlFileName.getSystemId() + " is NOT valid"); } catch (SAXException e) { e.printStackTrace(); System.out.println(xmlFileName.getSystemId() + " is NOT valid"); } </code></pre> <p>XSD File:</p> <pre><code>&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:element name="innovations"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element type="xs:string" name="organization" /&gt; &lt;xs:element name="innovation" maxOccurs="unbounded" minOccurs="1"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="title" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;xs:element name="brief_description" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;xs:element name="categories" minOccurs="1"&gt; &lt;xs:complexType mixed="true"&gt; &lt;xs:sequence&gt; &lt;xs:element name="category" maxOccurs="unbounded" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="tags" minOccurs="1"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="tag" maxOccurs="unbounded" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="full_description" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;xs:element name="patent_number" maxOccurs="unbounded" minOccurs="0"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;xs:element name="patent_status" maxOccurs="unbounded" minOccurs="0"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;xs:element name="case_manager" minOccurs="1"&gt; &lt;xs:complexType&gt; &lt;xs:simpleContent&gt; &lt;xs:extension base="xs:string"&gt; &lt;xs:attribute type="xs:string" name="first_name" use="optional" /&gt; &lt;xs:attribute type="xs:string" name="last_name" use="optional" /&gt; &lt;/xs:extension&gt; &lt;/xs:simpleContent&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="status" minOccurs="1"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:string"&gt; &lt;xs:minLength value="1" /&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;xs:attribute type="xs:short" name="file_number" use="required" /&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; </code></pre> <p></p> <p>Validation Event Handler Class</p> <pre><code>public class MyValidationEventHandler implements ValidationEventHandler { public boolean handleEvent(ValidationEvent event) { System.out.println("\nEVENT"); switch (event.getSeverity()) { case 0: System.out.println("SEVERITY: WARNING"); break; case 1: System.out.println("SEVERITY: ERROR"); break; case 2: System.out.println("SEVERITY: FATAL_ERROR"); break; } System.out.println("MESSAGE: " + event.getMessage()); System.out.println("LINKED EXCEPTION: " + event.getLinkedException()); System.out.println("LOCATOR"); System.out.println(" LINE NUMBER: " + event.getLocator().getLineNumber()); System.out.println(" COLUMN NUMBER: " + event.getLocator().getColumnNumber()); System.out.println(" OFFSET: " + event.getLocator().getOffset()); System.out.println(" OBJECT: " + event.getLocator().getObject()); System.out.println(" NODE: " + event.getLocator().getNode()); System.out.println(" URL: " + event.getLocator().getURL()); return true; } </code></pre> <p>}</p> <h2>Output:</h2> <p>EVENT SEVERITY: FATAL_ERROR MESSAGE: cvc-complex-type.2.4.a: Invalid content was found starting with element 'case_manager1'. One of '{patent_status, case_manager}' is expected. LINKED EXCEPTION: org.xml.sax.SAXParseException; systemId: file:/D:/AHWeb2.0.3/test3.xml; lineNumber: 24; columnNumber: 55; cvc-complex-type.2.4.a: Invalid content was found starting with element 'case_manager1'. One of '{patent_status, case_manager}' is expected. LOCATOR LINE NUMBER: 24 COLUMN NUMBER: 55 OFFSET: -1 OBJECT: null NODE: null URL: file:/D:/AHWeb2.0.3/test3.xml</p> <p>EVENT SEVERITY: ERROR MESSAGE: unexpected element (uri:"", local:"case_manager1"). Expected elements are &lt;{}tags>,&lt;{}title>,&lt;{}patent_number>,&lt;{}brief_description>,&lt;{}status>,&lt;{}full_description>,&lt;{}categories>,&lt;{}case_manager>,&lt;{}patent_status> LINKED EXCEPTION: null LOCATOR LINE NUMBER: 24 COLUMN NUMBER: 55 OFFSET: -1 OBJECT: null NODE: null URL: file:/D:/AHWeb2.0.3/test3.xml</p> <p>EVENT SEVERITY: FATAL_ERROR MESSAGE: The element type "case_manager1" must be terminated by the matching end-tag "". LINKED EXCEPTION: org.xml.sax.SAXParseException; systemId: file:/D:/AHWeb2.0.3/test3.xml; lineNumber: 25; columnNumber: 5; The element type "case_manager1" must be terminated by the matching end-tag "". LOCATOR LINE NUMBER: 25 COLUMN NUMBER: 5 OFFSET: -1 OBJECT: null NODE: null URL: file:/D:/AHWeb2.0.3/test3.xml file:/D:/AHWeb2.0.3/test3.xml is NOT valid javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; systemId: file:/D:/AHWeb2.0.3/test3.xml; lineNumber: 25; columnNumber: 5; The element type "case_manager1" must be terminated by the matching end-tag "".] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at com.test.innovation.validation.InnovationUnmarshalDemo.main(InnovationUnmarshalDemo.java:34) Caused by: org.xml.sax.SAXParseException; systemId: file:/D:/AHWeb2.0.3/test3.xml; lineNumber: 25; columnNumber: 5; The element type "case_manager1" must be terminated by the matching end-tag "". at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211) ... 4 more</p> <p>Thanks.</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