kailash gaur
kailash gaur

Reputation: 1447

UnmarshalException when doing unmarshling of xml

Exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://services.forddirect.fordvehicles.com/searchTest", local:"ClassName"). Expected elements are (none)

I am pasting the code part by which I am doing unmarshling of xml

try {

        File file = new File("ReflectionInput.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(SearchTest.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        SearchTest className = (SearchTest) jaxbUnmarshaller
                .unmarshal(file);
        System.out.println(className);

    } catch(JAXBException e) {
        e.printStackTrace();
    }

}

getting exception when unmarshalling from xml.

Exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://services.forddirect.fordvehicles.com/searchTest", local:"ClassName"). Expected elements are (none)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1052)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:464)
 at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:152)
 at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)

xsd file

<?xml version="1.0" encoding="utf-8"?>
 <xs:schema xmlns="http://services.forddirect.fordvehicles.com/searchTest"
  attributeFormDefault="unqualified" elementFormDefault="qualified"
  targetNamespace="http://services.forddirect.fordvehicles.com/searchTest"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="SearchTest" type="searchTest">
  </xs:element>
  <xs:complexType name="searchTest">
   <xs:sequence>
    <xs:element name="ClassName" type="className" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>

  <xs:complexType name="className">
   <xs:sequence>
    <xs:element name="param" type="xs:string" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType> 
 </xs:schema>

xml file

     <?xml version="1.0" encoding="UTF-8"?>
     <ClassName xmlns="http://services.forddirect.fordvehicles.com/searchTest">
     <param name ="make">Ford</param>
       <param name ="model">Mustang</param>
       <param name ="year">2011</param>
       <param name ="dealerPACode">2011</param>
       <param name ="segment">car</param>
       <param name ="segmentdefiner">NGP_ModelInfo_VehicleType</param>
       <param name           ="configtoken">Config[|Ford|Mustang|2014|1|1.|100A.P8A.....COU.~YZKAA.V6.LESS.]</param>
  <param name ="vin">3FA6P0H79DR338449</param>
     </ClassName>

Upvotes: 1

Views: 165

Answers (1)

bdoughan
bdoughan

Reputation: 149037

What the Problem is:

Because SearchTest is a global element with a named complex type, an @XmlRootElement annotation is not generated for the SearchTest class. Instead an @XmlElementDecl annotation is generated for it on the ObjectFactory class. This is because in JAXB classes correspond to complex types, and named complex types may have more that one global element that corresponds to it.

  <xs:element name="SearchTest" type="searchTest">
  </xs:element>
  <xs:complexType name="searchTest">
   <xs:sequence>
    <xs:element name="ClassName" type="className" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>

How to Fix it:

When creating a JAXBContext on a model generated from an XML Schema, you should create it on the ObjectFactory class or the package name of the generated model instead of an individual model class to get all the necessary metadata.

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);

For More Information

Upvotes: 0

Related Questions