title: ClassCastException when converting XML to POJO
body: <p>I am trying to convert an XML file to a POJO and I am getting the following error:</p>
  
  <pre><code>'java.lang.ClassCastException: com.ibm.xml.xlxp.api.stax.events.StartDocumentImpl incompatible with javax.xml.stream.events.StartElement'
  </code></pre>
  
  <p>I am running RAD 8.5 with websphere 6.1. I am using Java 1.5 and JAXB. All appropriate jar files are in the WEB_INF lib folder.</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.xml.stream.XMLEventReader;
  import javax.xml.stream.XMLInputFactory;
  import javax.xml.stream.XMLStreamException;
  import javax.xml.stream.events.Attribute;
  import javax.xml.stream.events.EndElement;
  import javax.xml.stream.events.StartElement;
  import javax.xml.stream.events.XMLEvent;
  
  import com.cvscaremark.eccr.domain.XMLInteraction;
  
  public class XMLParseUtil {
  
  static final String INTERACTION = "INTERACTION";
  static final String SESSIONID = "SESSION_ID";
  static final String CHANNEL = "CHAN_TYPE_CD";
  static final String APPLICATION = "SYS_ID";
  static final String INTERACTIONSOURCE = "CLT_CHAN_ID";
  static final String INTERACTIONINIT = "MBR_PRTY_ID_1_LEVEL";
  static final String CATEGORY = "SRVC_ID";
  static final String TYPE = "INTRCTN_TYPE_CD";
  static final String DATETIME = "CRT_TS";
  static final String CONTACTID = "CORP_CHAN_ID";
  static final String REFERENCEID = "RFRNC_KEY_ID";
  static final String REFERENCENB = "RFRNC_KEY_NB";
  static final String STATUS = "INTRCTN_RESULT_CD";
  
  public List&lt;XMLInteraction&gt; readXML(String XMLFile){
  
      List&lt;XMLInteraction&gt; XMLInteractions = new ArrayList&lt;XMLInteraction&gt;();
  
  
      try {
  
          // First create a new XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Setup a new eventReader
            InputStream in = new FileInputStream(XMLFile);
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
            // Read the XML document
            XMLInteraction xmlInteraction = null;
  
            while (eventReader.hasNext()) {
                  XMLEvent event = eventReader.nextEvent();
  
                  if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    // If we have a interaction element we create a new interaction
                    if (startElement.getName().getLocalPart() == (INTERACTION)) {
                        xmlInteraction = new XMLInteraction();
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(SESSIONID) &amp;&amp; xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setSessionID(event.asCharacters().getData());
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(CHANNEL) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setChannel(event.asCharacters().getData());
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(APPLICATION) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setApplication(event.asCharacters().getData());
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(INTERACTIONSOURCE) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setInteraction_Source(event.asCharacters().getData());
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(INTERACTIONINIT) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setInteraction_Initiator(event.asCharacters().getData());
                    }
                    if (event.asStartElement().getName().getLocalPart().equals(CATEGORY) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setCategory(event.asCharacters().getData());
                        }
                    if (event.asStartElement().getName().getLocalPart().equals(TYPE) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                        }                   
                    if (event.asStartElement().getName().getLocalPart().equals(DATETIME) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setDateTime(event.asCharacters().getData());
                        }
                    if (event.asStartElement().getName().getLocalPart().equals(CONTACTID) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setContactID(event.asCharacters().getData());
                        }
                    if (event.asStartElement().getName().getLocalPart().equals(REFERENCEID) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setReference(event.asCharacters().getData());
                          continue;
                        }
                    if (event.asStartElement().getName().getLocalPart().equals(STATUS) &amp;&amp; xmlInteraction != null) {
                          event = eventReader.nextEvent();
                          xmlInteraction.setResultStatus(event.asCharacters().getData());
                          continue;
                        }
  
  
          }
  
  
  
                      // If we reach the end of an item element we add it to the list
                      if (event.isEndElement()) {
                        EndElement endElement = event.asEndElement();
                        if (endElement.getName().getLocalPart() == (INTERACTION)) {
                          XMLInteractions.add(xmlInteraction);
                        }
                      }
  
  
  
      }
      }
  
           catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (XMLStreamException e) {
            e.printStackTrace();
          }
  
  
  
  
  
  
  
  return XMLInteractions;
  }
  </code></pre>
  
  <p>}    </p>
  
  <p>Here is my POJO:</p>
  
  <pre><code>import java.util.ArrayList;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlType;
  
  
  
  @XmlRootElement(name = "INTERACTION")
  public class XMLInteraction {
  
  private String sessionID;
  private String channel;
  private String application;
  private String interaction_Source;
  private String interaction_Initiator;
  private String category;
  private String type;
  private String dateTime;
  private String contactID;
  private String reference;
  private String resultStatus;
  
  /**
   * @return
   */
  public String getSessionID() {
      return sessionID;
  }
  /**
   * @param sessionID
   */
  @XmlElement(name="SESSION_ID")
  public void setSessionID(String sessionID) {
      this.sessionID = sessionID;
  }
  /**
   * @return
   */
  public String getChannel() {
      return channel;
  }
  /**
   * @param channel
   */
  @XmlElement(name="CHAN_TYPE_CD")
  public void setChannel(String channel) {
      this.channel = channel;
  }
  /**
   * @return
   */
  public String getApplication() {
      return application;
  }
  /**
   * @param application
   */
  @XmlElement(name="SYS_ID")
  public void setApplication(String application) {
      this.application = application;
  }
  /**
   * @return
   */
  public String getInteraction_Source() {
      return interaction_Source;
  }
  /**
   * @param interaction_Source
   */
  @XmlElement(name="CLT_CHAN_ID")
  public void setInteraction_Source(String interaction_Source) {
      this.interaction_Source = interaction_Source;
  }
  /**
   * @return
   */
  public String getInteraction_Initiator() {
      return interaction_Initiator;
  }
  /**
   * @param interaction_Initiator
   */
  @XmlElement(name="MBR_PRTY_ID_1_LEVEL")
  public void setInteraction_Initiator(String interaction_Initiator) {
      this.interaction_Initiator = interaction_Initiator;
  }
  /**
   * @return the category
   */
  public String getCategory() {
      return category;
  }
  /**
   * @param category the category to set
   */
  @XmlElement(name="SRVC_ID")
  public void setCategory(String category) {
      this.category = category;
  }
  /**
   * @return the type
   */
  public String getType() {
      return type;
  }
  /**
   * @param type the type to set
   */
  @XmlElement(name="INTRCTN_TYPE_CD")
  public void setType(String type) {
      this.type = type;
  }
  /**
   * @return the dateTime
   */
  public String getDateTime() {
      return dateTime;
  }
  /**
   * @param dateTime the dateTime to set
   */
  @XmlElement(name="CRT_TS")
  public void setDateTime(String dateTime) {
      this.dateTime = dateTime;
  }
  /**
   * @return the contactID
   */
  public String getContactID() {
      return contactID;
  }
  /**
   * @param contactID the contactID to set
   */
  @XmlElement(name="CORP_CHAN_ID")
  public void setContactID(String contactID) {
      this.contactID = contactID;
  }
  /**
   * @return the reference
   */
  public String getReference() {
      return reference;
  }
  /**
   * @param reference the reference to set
   */
  @XmlElement(name="RFRNC_KEY_NB")
  public void setReference(String reference) {
      this.reference = reference;
  }
  /**
   * @return the resultStatus
   */
  public String getResultStatus() {
      return resultStatus;
  }
  /**
   * @param resultStatus the resultStatus to set
   */
  @XmlElement(name="INTRCTN_RESULT_CD")
  public void setResultStatus(String resultStatus) {
      this.resultStatus = resultStatus;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;INTERACTIONS&gt;
  &lt;INTERACTION&gt;
    &lt;INTRCTN_KEY_ID&gt;Id-006a5fa551b63f4c01007365&lt;/INTRCTN_KEY_ID&gt;
    &lt;TRX_SEQ_NB&gt;1&lt;/TRX_SEQ_NB&gt;
    &lt;MBR_SEQ_NB&gt;1&lt;/MBR_SEQ_NB&gt;
    &lt;MBR_ID_1_LEVEL&gt;13344&lt;/MBR_ID_1_LEVEL&gt;
    &lt;SESSION_ID&gt;12345&lt;/SESSION_ID&gt;
    &lt;CHAN_TYPE_CD&gt;&lt;/CHAN_TYPE_CD&gt;
    &lt;SYS_ID&gt;CMP&lt;/SYS_ID&gt;
    &lt;CLT_CHAN_ID&gt;&lt;/CLT_CHAN_ID&gt;
    &lt;MBR_PRTY_ID_1_LEVEL&gt;&lt;/MBR_PRTY_ID_1_LEVEL&gt;
    &lt;SRVC_ID&gt;&lt;/SRVC_ID&gt;
    &lt;INTRCTN_TYPE_CD&gt;Unknown&lt;/INTRCTN_TYPE_CD&gt;
    &lt;CRT_TS&gt;2013-6-10.14.4. 12. 352398000&lt;/CRT_TS&gt;
    &lt;CORP_CHAN_ID&gt;&lt;/CORP_CHAN_ID&gt;
    &lt;RFRNC_KEY_ID&gt;54731123&lt;/RFRNC_KEY_ID&gt;
    &lt;RFRNC_KEY_NB&gt;1234&lt;/RFRNC_KEY_NB&gt;
    &lt;INTRCTN_RESULT_CD&gt;Unknown&lt;/INTRCTN_RESULT_CD&gt;
  &lt;/INTERACTION&gt;
  
  &lt;INTERACTION&gt;
    &lt;INTRCTN_KEY_ID&gt;Id-006a5fa551b63f4c01007365&lt;/INTRCTN_KEY_ID&gt;
    &lt;TRX_SEQ_NB&gt;1&lt;/TRX_SEQ_NB&gt;
    &lt;MBR_SEQ_NB&gt;1&lt;/MBR_SEQ_NB&gt;
    &lt;MBR_ID_1_LEVEL&gt;13344&lt;/MBR_ID_1_LEVEL&gt;
    &lt;SESSION_ID&gt;12345&lt;/SESSION_ID&gt;
    &lt;CHAN_TYPE_CD&gt;&lt;/CHAN_TYPE_CD&gt;
    &lt;SYS_ID&gt;CMP&lt;/SYS_ID&gt;
    &lt;CLT_CHAN_ID&gt;&lt;/CLT_CHAN_ID&gt;
    &lt;MBR_PRTY_ID_1_LEVEL&gt;&lt;/MBR_PRTY_ID_1_LEVEL&gt;
    &lt;SRVC_ID&gt;&lt;/SRVC_ID&gt;
    &lt;INTRCTN_TYPE_CD&gt;Unknown&lt;/INTRCTN_TYPE_CD&gt;
    &lt;CRT_TS&gt;2013-6-10.14.4. 12. 352398000&lt;/CRT_TS&gt;
    &lt;CORP_CHAN_ID&gt;&lt;/CORP_CHAN_ID&gt;
    &lt;RFRNC_KEY_ID&gt;54731123&lt;/RFRNC_KEY_ID&gt;
    &lt;RFRNC_KEY_NB&gt;1234&lt;/RFRNC_KEY_NB&gt;
    &lt;INTRCTN_RESULT_CD&gt;Unknown&lt;/INTRCTN_RESULT_CD&gt;
  &lt;/INTERACTION&gt;
  
  &lt;INTRACTION&gt;
    &lt;INTRCTN_KEY_ID&gt;Id-006a5fa551b63f4c01007365&lt;/INTRCTN_KEY_ID&gt;
    &lt;TRX_SEQ_NB&gt;1&lt;/TRX_SEQ_NB&gt;
    &lt;MBR_SEQ_NB&gt;1&lt;/MBR_SEQ_NB&gt;
    &lt;MBR_ID_1_LEVEL&gt;13344&lt;/MBR_ID_1_LEVEL&gt;
    &lt;SESSION_ID&gt;12345&lt;/SESSION_ID&gt;
    &lt;CHAN_TYPE_CD&gt;&lt;/CHAN_TYPE_CD&gt;
    &lt;SYS_ID&gt;CMP&lt;/SYS_ID&gt;
    &lt;CLT_CHAN_ID&gt;&lt;/CLT_CHAN_ID&gt;
    &lt;MBR_PRTY_ID_1_LEVEL&gt;&lt;/MBR_PRTY_ID_1_LEVEL&gt;
    &lt;SRVC_ID&gt;&lt;/SRVC_ID&gt;
    &lt;INTRCTN_TYPE_CD&gt;Unknown&lt;/INTRCTN_TYPE_CD&gt;
    &lt;CRT_TS&gt;2013-6-10.14.4. 12. 352398000&lt;/CRT_TS&gt;
    &lt;CORP_CHAN_ID&gt;&lt;/CORP_CHAN_ID&gt;
    &lt;RFRNC_KEY_ID&gt;54731123&lt;/RFRNC_KEY_ID&gt;
    &lt;RFRNC_KEY_NB&gt;1234&lt;/RFRNC_KEY_NB&gt;
    &lt;INTRCTN_RESULT_CD&gt;Unknown&lt;/INTRCTN_RESULT_CD&gt;
  &lt;/INTERACTION&gt;
  
  &lt;INTERACTION&gt;
    &lt;INTRCTN_KEY_ID&gt;Id-006a5fa551b63f4c01007365&lt;/INTRCTN_KEY_ID&gt;
    &lt;TRX_SEQ_NB&gt;1&lt;/TRX_SEQ_NB&gt;
    &lt;MBR_SEQ_NB&gt;1&lt;/MBR_SEQ_NB&gt;
    &lt;MBR_ID_1_LEVEL&gt;13344&lt;/MBR_ID_1_LEVEL&gt;
    &lt;SESSION_ID&gt;12345&lt;/SESSION_ID&gt;
    &lt;CHAN_TYPE_CD&gt;&lt;/CHAN_TYPE_CD&gt;
    &lt;SYS_ID&gt;CMP&lt;/SYS_ID&gt;
    &lt;CLT_CHAN_ID&gt;&lt;/CLT_CHAN_ID&gt;
    &lt;MBR_PRTY_ID_1_LEVEL&gt;&lt;/MBR_PRTY_ID_1_LEVEL&gt;
    &lt;SRVC_ID&gt;&lt;/SRVC_ID&gt;
    &lt;INTRCTN_TYPE_CD&gt;Unknown&lt;/INTRCTN_TYPE_CD&gt;
    &lt;CRT_TS&gt;2013-6-10.14.4. 12. 352398000&lt;/CRT_TS&gt;
    &lt;CORP_CHAN_ID&gt;&lt;/CORP_CHAN_ID&gt;
    &lt;RFRNC_KEY_ID&gt;54731123&lt;/RFRNC_KEY_ID&gt;
    &lt;RFRNC_KEY_NB&gt;1234&lt;/RFRNC_KEY_NB&gt;
    &lt;INTRCTN_RESULT_CD&gt;Unknown&lt;/INTRCTN_RESULT_CD&gt;
  &lt;/INTERACTION&gt;
  
  &lt;INTERACTION&gt;
    &lt;INTRCTN_KEY_ID&gt;Id-006a5fa551b63f4c01007365&lt;/INTRCTN_KEY_ID&gt;
    &lt;TRX_SEQ_NB&gt;1&lt;/TRX_SEQ_NB&gt;
    &lt;MBR_SEQ_NB&gt;1&lt;/MBR_SEQ_NB&gt;
    &lt;MBR_ID_1_LEVEL&gt;13344&lt;/MBR_ID_1_LEVEL&gt;
    &lt;SESSION_ID&gt;12345&lt;/SESSION_ID&gt;
    &lt;CHAN_TYPE_CD&gt;&lt;/CHAN_TYPE_CD&gt;
    &lt;SYS_ID&gt;CMP&lt;/SYS_ID&gt;
    &lt;CLT_CHAN_ID&gt;&lt;/CLT_CHAN_ID&gt;
    &lt;MBR_PRTY_ID_1_LEVEL&gt;&lt;/MBR_PRTY_ID_1_LEVEL&gt;
    &lt;SRVC_ID&gt;&lt;/SRVC_ID&gt;
    &lt;INTRCTN_TYPE_CD&gt;Unknown&lt;/INTRCTN_TYPE_CD&gt;
    &lt;CRT_TS&gt;2013-6-10.14.4. 12. 352398000&lt;/CRT_TS&gt;
    &lt;CORP_CHAN_ID&gt;&lt;/CORP_CHAN_ID&gt;
    &lt;RFRNC_KEY_ID&gt;54731123&lt;/RFRNC_KEY_ID&gt;
    &lt;RFRNC_KEY_NB&gt;1234&lt;/RFRNC_KEY_NB&gt;
    &lt;INTRCTN_RESULT_CD&gt;Unknown&lt;/INTRCTN_RESULT_CD&gt;
  &lt;/INTERACTION&gt;
  &lt;/INTERACTIONS&gt;
  </code></pre>
  
  <p>I do not have the possibility of an XSD or any type of schema. When the Web App is deployed it will hit a 'URL' and receive back an XML document similar to the one above.</p>
  
  <p>As an extra question, <em>is there any way to unmarshall the XML into several nested POJOS without using Castor etc but just using JAXB?</em></p>
  
  <p>Even though the XML is supplied as a flat document I need to place the information into several POJOS which can be contained in another POJO. For example the structure would look something like:</p>
  
  <ul>
  <li><p>An InteractionList would contain multiple Interactions.</p>
  
  <p>An Interaction can contain several Transactions. </p>
  
  <p>A Transaction can have multiple References and Members.</p></li>
  </ul>
  
  <p>Again I must derive this from the XML as above.</p>
  
  <p>Many thanks in advance.</p>
  
  <p>The full exception is as follows:</p>
  
  <pre><code>java.lang.ClassCastException: com.ibm.xml.xlxp.api.stax.events.StartDocumentImpl     
  incompatible with javax.xml.stream.events.StartElement
  at 
  com.ibm.xml.xlxp.api.stax.events.XMLEventImpl.asStartElement(XMLEventImpl.java:239)
  at com.cvscaremark.eccr.web.utilities.XMLParseUtil.readXML(XMLParseUtil.java:56)
  at 
  
  test.java.com.cvscaremark.eccr.web.utilities.XMLParseUtilTest.testParseXML
  (XMLParseUtilTest.java:24)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
  at  
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:615)
  at 
  org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at 
  org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at 
  org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at 
   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
  (JUnit4TestReference.java:49)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at 
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
  (RemoteTestRunner.java:467)
  at 
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
  (RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
  (RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
  (RemoteTestRunner.java:197)
  </code></pre>
  
  <p>The exception occurs at:
       XMLParseUtil
       StartElement startElement = event.asStartElement();</p>
  
  <p>and atthe linw below marked with the stars:</p>
  
  <pre><code>package test.java.com.cvscaremark.eccr.web.utilities;
  
  
  import java.util.List;
  
  
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.junit.runners.JUnit4;
  
  import com.cvscaremark.eccr.domain.XMLInteraction;
  import com.cvscaremark.eccr.web.utilities.XMLParseUtil;
  
  @RunWith(JUnit4.class)
  public class XMLParseUtilTest {
  
  
  
  @Test
  public void testParseXML(){
  
      XMLParseUtil read = new XMLParseUtil();
    ***  List&lt;XMLInteraction&gt; readXML = read.readXML("C:\\file.xml"); ***
      for (XMLInteraction xmlInteraction : readXML) {
        System.out.println(xmlInteraction);
  
  }
  }
  </code></pre>
  
  <p>}</p>