有和没有JAXBElement的jaxb unmarshaller

时间:2012-11-27 20:10:15

标签: java jaxb unmarshalling

我有以下JAXB注释类(使用XJC生成)

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ReplicateTransactionDataNotificationType", namespace = "http://esb.pkobp/adapter/calypso/tradekast/transaction", propOrder = {
        "operationContext",
        "transactionData"
    })
    public class ReplicateTransactionDataNotificationType
        implements Serializable
    {

        @XmlElement(name = "OperationContext", required = true)
        protected OperationContextType operationContext;
        @XmlElement(name = "TransactionData")
        protected TransactionDataType transactionData;

        public OperationContextType getOperationContext() {
            return operationContext;
        }

        public void setOperationContext(OperationContextType value) {
            this.operationContext = value;
        }

        public TransactionDataType getTransactionData() {
            return transactionData;
        }

        public void setTransactionData(TransactionDataType value) {
            this.transactionData = value;
        }

    }

当以下列方式解组时,一切正常:

JAXBContext ctx = JAXBContext.newInstance(ReplicateTransactionDataNotificationType.class);
    Unmarshaller unmarshaller =  ctx.createUnmarshaller();
    JAXBElement<ReplicateTransactionDataNotificationType> result;
    result = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xmlString.getBytes())), ReplicateTransactionDataNotificationType.class);

以下列方式解组时,它会以异常

崩溃
(ReplicateTransactionDataNotificationType)unmarshaller.unmarshal(new StringReader(xmlString));

例外:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://esb.pkobp/adapter/calypso/tradekast/transaction", local:"caltradetran_ReplicateTransactionDataNotification"). Expected elements are (none)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    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)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
    at myPackage.XMLTranslationResolver.main(KondorXMLTranslationResolver.java:120)

sombody可以解释这种奇怪的行为吗?

正在解组的XML文件粘贴在下面:

<?xml version="1.0" encoding="UTF-8"?>
<caltradetran:caltradetran_ReplicateTransactionDataNotification xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                                                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                                                                xmlns:caltradetran="http://esb.pkobp/adapter/calypso/tradekast/transaction"
                                                                xmlns:tran="http://esb.pkobp/cdm/treasury/transaction">
   <OperationContext>
      <RequestID>98573c9c-6167-4edb-a5ff-8ad3db9983ef</RequestID>
      <EventSource>KONDOR</EventSource>
      <EventDate>2012-11-13T12:13:42.937+01:00</EventDate>
   </OperationContext>
   <TransactionData>
      <ExternalReferenceKp>FWD57658</ExternalReferenceKp>
      <DealTableID>95</DealTableID>
      <Action>INS</Action>
      <TransactionAdditionalData>
         <ExternalReferenceKp>FWD57658</ExternalReferenceKp>
         <ExternalReferenceTr/>
         <ClientPID>123456</ClientPID>
         <ProductSubType>FXForward</ProductSubType>
         <SourceSystem>Kondor+</SourceSystem>
         <AdditionalParameterList>
            <Parameter>
               <Name>TransactionType</Name>
               <ValueType>string</ValueType>
               <StringValue>Spekulacja</StringValue>
            </Parameter>
            <Parameter>
               <Name>ReceiveFrom_AccountNo</Name>
               <ValueType>string</ValueType>
               <StringValue>test</StringValue>
            </Parameter>
            <Parameter>
               <Name>ReceiveFrom_SettleMethod</Name>
               <ValueType>string</ValueType>
               <StringValue>DIRECT</StringValue>
            </Parameter>
            <Parameter>
               <Name>ReceiveTo_AccountNo</Name>
               <ValueType>string</ValueType>
               <StringValue>test</StringValue>
            </Parameter>
            <Parameter>
               <Name>ReceiveTo_SettleMethod</Name>
               <ValueType>string</ValueType>
               <StringValue>DIRECT</StringValue>
            </Parameter>
            <Parameter>
               <Name>PayFrom_AccountNo</Name>
               <ValueType>string</ValueType>
               <StringValue>test</StringValue>
            </Parameter>
            <Parameter>
               <Name>PayFrom_SettleMethod</Name>
               <ValueType>string</ValueType>
               <StringValue>DIRECT</StringValue>
            </Parameter>
            <Parameter>
               <Name>PayTo_AccountNo</Name>
               <ValueType>string</ValueType>
               <StringValue>test</StringValue>
            </Parameter>
            <Parameter>
               <Name>PayTo_SettleMethod</Name>
               <ValueType>string</ValueType>
               <StringValue>DIRECT</StringValue>
            </Parameter>
         </AdditionalParameterList>
      </TransactionAdditionalData>
      <ForwardDeal>
         <FILE>1105_133831_INS.00057658.00072904</FILE>
         <ACTION>INS</ACTION>
         <TRANS_ID>72904</TRANS_ID>
         <XACTKEY>350000000353842604</XACTKEY>
         <OPERATIONS>3</OPERATIONS>
         <MAIN_DEAL_TABLE_ID>95</MAIN_DEAL_TABLE_ID>
         <MAIN_DEAL_ID>57658</MAIN_DEAL_ID>
         <ORIGINAL_TYPE_OF_EVENT>K</ORIGINAL_TYPE_OF_EVENT>
         <DEALS_INSTRUMENT_ID>13</DEALS_INSTRUMENT_ID>
         <DEALS_OPERATION_ID>0</DEALS_OPERATION_ID>
         <MESSAGE_SOURCE>RealTime</MESSAGE_SOURCE>
         <DEALS_OPERATION>INSERT</DEALS_OPERATION>
      </ForwardDeal>
   </TransactionData>
</caltradetran:caltradetran_ReplicateTransactionDataNotification>

1 个答案:

答案 0 :(得分:4)

创建ObjectFactory时,您需要包含JAXBContext课程。 ObjectFactory包含@XmlElementDecl注释,其中包含根元素信息。我建议在生成的模型的包名称上创建JAXBContext

了解更多信息