1

我正在尝试将 XML 文件转换为 POJO,但出现以下错误:

'java.lang.ClassCastException: com.ibm.xml.xlxp.api.stax.events.StartDocumentImpl incompatible with javax.xml.stream.events.StartElement'

我正在使用 websphere 6.1 运行 RAD 8.5。我正在使用 Java 1.5 和 JAXB。所有合适的 jar 文件都在 WEB_INF lib 文件夹中。

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<XMLInteraction> readXML(String XMLFile){

    List<XMLInteraction> XMLInteractions = new ArrayList<XMLInteraction>();


    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) && xmlInteraction != null) {
                      event = eventReader.nextEvent();
                      xmlInteraction.setSessionID(event.asCharacters().getData());
                  }
                  if (event.asStartElement().getName().getLocalPart().equals(CHANNEL) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setChannel(event.asCharacters().getData());
                  }
                  if (event.asStartElement().getName().getLocalPart().equals(APPLICATION) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setApplication(event.asCharacters().getData());
                  }
                  if (event.asStartElement().getName().getLocalPart().equals(INTERACTIONSOURCE) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setInteraction_Source(event.asCharacters().getData());
                  }
                  if (event.asStartElement().getName().getLocalPart().equals(INTERACTIONINIT) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setInteraction_Initiator(event.asCharacters().getData());
                  }
                  if (event.asStartElement().getName().getLocalPart().equals(CATEGORY) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setCategory(event.asCharacters().getData());
                      }
                  if (event.asStartElement().getName().getLocalPart().equals(TYPE) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                      }                   
                  if (event.asStartElement().getName().getLocalPart().equals(DATETIME) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setDateTime(event.asCharacters().getData());
                      }
                  if (event.asStartElement().getName().getLocalPart().equals(CONTACTID) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setContactID(event.asCharacters().getData());
                      }
                  if (event.asStartElement().getName().getLocalPart().equals(REFERENCEID) && xmlInteraction != null) {
                        event = eventReader.nextEvent();
                        xmlInteraction.setReference(event.asCharacters().getData());
                        continue;
                      }
                  if (event.asStartElement().getName().getLocalPart().equals(STATUS) && 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;
}

}

这是我的 POJO:

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;
}

}

这是我的 XML:

<?xml version="1.0"?>
<INTERACTIONS>
<INTERACTION>
  <INTRCTN_KEY_ID>Id-006a5fa551b63f4c01007365</INTRCTN_KEY_ID>
  <TRX_SEQ_NB>1</TRX_SEQ_NB>
  <MBR_SEQ_NB>1</MBR_SEQ_NB>
  <MBR_ID_1_LEVEL>13344</MBR_ID_1_LEVEL>
  <SESSION_ID>12345</SESSION_ID>
  <CHAN_TYPE_CD></CHAN_TYPE_CD>
  <SYS_ID>CMP</SYS_ID>
  <CLT_CHAN_ID></CLT_CHAN_ID>
  <MBR_PRTY_ID_1_LEVEL></MBR_PRTY_ID_1_LEVEL>
  <SRVC_ID></SRVC_ID>
  <INTRCTN_TYPE_CD>Unknown</INTRCTN_TYPE_CD>
  <CRT_TS>2013-6-10.14.4. 12. 352398000</CRT_TS>
  <CORP_CHAN_ID></CORP_CHAN_ID>
  <RFRNC_KEY_ID>54731123</RFRNC_KEY_ID>
  <RFRNC_KEY_NB>1234</RFRNC_KEY_NB>
  <INTRCTN_RESULT_CD>Unknown</INTRCTN_RESULT_CD>
</INTERACTION>

<INTERACTION>
  <INTRCTN_KEY_ID>Id-006a5fa551b63f4c01007365</INTRCTN_KEY_ID>
  <TRX_SEQ_NB>1</TRX_SEQ_NB>
  <MBR_SEQ_NB>1</MBR_SEQ_NB>
  <MBR_ID_1_LEVEL>13344</MBR_ID_1_LEVEL>
  <SESSION_ID>12345</SESSION_ID>
  <CHAN_TYPE_CD></CHAN_TYPE_CD>
  <SYS_ID>CMP</SYS_ID>
  <CLT_CHAN_ID></CLT_CHAN_ID>
  <MBR_PRTY_ID_1_LEVEL></MBR_PRTY_ID_1_LEVEL>
  <SRVC_ID></SRVC_ID>
  <INTRCTN_TYPE_CD>Unknown</INTRCTN_TYPE_CD>
  <CRT_TS>2013-6-10.14.4. 12. 352398000</CRT_TS>
  <CORP_CHAN_ID></CORP_CHAN_ID>
  <RFRNC_KEY_ID>54731123</RFRNC_KEY_ID>
  <RFRNC_KEY_NB>1234</RFRNC_KEY_NB>
  <INTRCTN_RESULT_CD>Unknown</INTRCTN_RESULT_CD>
</INTERACTION>

<INTRACTION>
  <INTRCTN_KEY_ID>Id-006a5fa551b63f4c01007365</INTRCTN_KEY_ID>
  <TRX_SEQ_NB>1</TRX_SEQ_NB>
  <MBR_SEQ_NB>1</MBR_SEQ_NB>
  <MBR_ID_1_LEVEL>13344</MBR_ID_1_LEVEL>
  <SESSION_ID>12345</SESSION_ID>
  <CHAN_TYPE_CD></CHAN_TYPE_CD>
  <SYS_ID>CMP</SYS_ID>
  <CLT_CHAN_ID></CLT_CHAN_ID>
  <MBR_PRTY_ID_1_LEVEL></MBR_PRTY_ID_1_LEVEL>
  <SRVC_ID></SRVC_ID>
  <INTRCTN_TYPE_CD>Unknown</INTRCTN_TYPE_CD>
  <CRT_TS>2013-6-10.14.4. 12. 352398000</CRT_TS>
  <CORP_CHAN_ID></CORP_CHAN_ID>
  <RFRNC_KEY_ID>54731123</RFRNC_KEY_ID>
  <RFRNC_KEY_NB>1234</RFRNC_KEY_NB>
  <INTRCTN_RESULT_CD>Unknown</INTRCTN_RESULT_CD>
</INTERACTION>

<INTERACTION>
  <INTRCTN_KEY_ID>Id-006a5fa551b63f4c01007365</INTRCTN_KEY_ID>
  <TRX_SEQ_NB>1</TRX_SEQ_NB>
  <MBR_SEQ_NB>1</MBR_SEQ_NB>
  <MBR_ID_1_LEVEL>13344</MBR_ID_1_LEVEL>
  <SESSION_ID>12345</SESSION_ID>
  <CHAN_TYPE_CD></CHAN_TYPE_CD>
  <SYS_ID>CMP</SYS_ID>
  <CLT_CHAN_ID></CLT_CHAN_ID>
  <MBR_PRTY_ID_1_LEVEL></MBR_PRTY_ID_1_LEVEL>
  <SRVC_ID></SRVC_ID>
  <INTRCTN_TYPE_CD>Unknown</INTRCTN_TYPE_CD>
  <CRT_TS>2013-6-10.14.4. 12. 352398000</CRT_TS>
  <CORP_CHAN_ID></CORP_CHAN_ID>
  <RFRNC_KEY_ID>54731123</RFRNC_KEY_ID>
  <RFRNC_KEY_NB>1234</RFRNC_KEY_NB>
  <INTRCTN_RESULT_CD>Unknown</INTRCTN_RESULT_CD>
</INTERACTION>

<INTERACTION>
  <INTRCTN_KEY_ID>Id-006a5fa551b63f4c01007365</INTRCTN_KEY_ID>
  <TRX_SEQ_NB>1</TRX_SEQ_NB>
  <MBR_SEQ_NB>1</MBR_SEQ_NB>
  <MBR_ID_1_LEVEL>13344</MBR_ID_1_LEVEL>
  <SESSION_ID>12345</SESSION_ID>
  <CHAN_TYPE_CD></CHAN_TYPE_CD>
  <SYS_ID>CMP</SYS_ID>
  <CLT_CHAN_ID></CLT_CHAN_ID>
  <MBR_PRTY_ID_1_LEVEL></MBR_PRTY_ID_1_LEVEL>
  <SRVC_ID></SRVC_ID>
  <INTRCTN_TYPE_CD>Unknown</INTRCTN_TYPE_CD>
  <CRT_TS>2013-6-10.14.4. 12. 352398000</CRT_TS>
  <CORP_CHAN_ID></CORP_CHAN_ID>
  <RFRNC_KEY_ID>54731123</RFRNC_KEY_ID>
  <RFRNC_KEY_NB>1234</RFRNC_KEY_NB>
  <INTRCTN_RESULT_CD>Unknown</INTRCTN_RESULT_CD>
</INTERACTION>
</INTERACTIONS>

我没有 XSD 或任何类型的架构的可能性。部署 Web 应用程序后,它将点击一个“URL”并接收回一个与上述类似的 XML 文档。

作为一个额外的问题,有没有什么方法可以在不使用 Castor 等而只使用 JAXB 的情况下将 XML 解组为几个嵌套的 POJOS?

即使 XML 作为平面文档提供,我也需要将信息放入几个 POJO 中,这些 POJO 可以包含在另一个 POJO 中。例如,结构看起来像:

  • 一个 InteractionList 将包含多个交互。

    一个交互可以包含多个事务。

    一个事务可以有多个引用和成员。

同样,我必须从上面的 XML 中得到它。

提前谢谢了。

完整的例外情况如下:

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)

异常发生在: XMLParseUtil StartElement startElement = event.asStartElement();

并在下面标有星号的 linw 处:

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<XMLInteraction> readXML = read.readXML("C:\\file.xml"); ***
    for (XMLInteraction xmlInteraction : readXML) {
      System.out.println(xmlInteraction);

}
}

}

4

2 回答 2

0

看起来你有一个不合规的 StAX 实现。您的错误不应该发生并且是一个错误。

isStartElement()用方法检查事件后asStartElement()不应该抛出ClassCastException.

请同时使用另一个实现检查这一点并提交错误。

于 2013-07-03T12:27:08.330 回答
0

我会为 INTERACTIONS 开设一门课程。

@XmlRootElement(name = "INTERACTIONS")
public class Interactions {

    @XmlElementWrapper(name = "interactions")
    @XmlElement(name = "INTERACTION")
    ...

顺便说一句,JAXB 看起来更容易。

读写

public static <T> T loadXML(Class<T> klazz, File xmlFile) throws IOException {
    try {
        if (!xmlFile.canRead()) {
            throw new JAXBException("FileNotFound; " + xmlFile.getPath());
        }
        JAXBContext jaxbContext = JAXBContext.newInstance(klazz);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return klazz.cast(jaxbUnmarshaller.unmarshal(xmlFile));
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}

public static <T> void saveXML(Class<T> klazz, T object, File xmlFile) throws IOException {
    if (object == null) {
        return;
    }
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(klazz);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(object, xmlFile);
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}
于 2013-07-03T12:31:35.323 回答