How to ignore extra elements and attributes when unmarshalling xml using JiBX

Solution for:

org.jibx.runtime.JiBXException: Expected "{http://abc.net/xyz/1.0}Element1" end tag, found "{http://abc.net/xyz/1.0}Element2" start tag (line 6, col 14)
at org.jibx.runtime.impl.UnmarshallingContext.parsePastCurrentEndTag(UnmarshallingContext.java:731)
at com.gsicommerce.order.vo.order.JiBX_orderBindingOrder_access2.unmarshal()
at com.gsicommerce.order.vo.order.OrderCreateRequest.JiBX_orderBinding_unmarshal_1_0(OrderCreateRequest.java)
at com.gsicommerce.order.vo.order.JiBX_orderBindingOrderCreateRequest_access2.unmarshal()
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(UnmarshallingContext.java:2757)
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(UnmarshallingContext.java:2914)

One of the drawbacks of using JiBX binding is that it expects the incoming xml to have all the elements and attributes in the binding xml file. In a real world situation this is rarely the case. Developers frequently attempting to unmarshall the incoming xml into value objects don't have control over the content of the xml. The incoming xml could be generated by a webservice or a third party over which the developer would have no control. Unfortunately, when JiBX finds extra elements in the input xml it would throw an exception that looks like below:

org.jibx.runtime.JiBXException: Expected "{http://schema.gspt.net/Order/1.0}Order" end tag, found "{http://schema.gspt.net/Order/1.0}OrderItem" start tag (line 6, col 14)
at org.jibx.runtime.impl.UnmarshallingContext.parsePastCurrentEndTag(UnmarshallingContext.java:731)
at com.gsicommerce.order.vo.order.JiBX_orderBindingOrder_access2.unmarshal()
at com.gsicommerce.order.vo.order.OrderCreateRequest.JiBX_orderBinding_unmarshal_1_0(OrderCreateRequest.java)
at com.gsicommerce.order.vo.order.JiBX_orderBindingOrderCreateRequest_access2.unmarshal()
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(UnmarshallingContext.java:2757)
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(UnmarshallingContext.java:2914)

To avoid this issue, if there are extra elements in the input xml they should be mapped in the binding xml. If this is not possible the parent of the missing element should be declared with flexible=”true”

If there are extra attributes that are not mapped, jibx ignores them which would work in our favour.