2016-03-28 63 views
0

時忽略命名空間時,源文件與命名空間:如何解組XML文件

xmlns="http://schemas.alcatel.com/iptv/singtel" 

誤差將加薪:

Encountered an error executing step sma-updstbparams.processfile in job sma-updstbparams 
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException 
- with linked exception: 
[com.sun.istack.internal.SAXParseException2; lineNumber: 3; columnNumber: 24; unexpected element (uri:"http://schemas.alcatel.com/iptv/singtel", local:"InitializedDeviceBE"). Expected elements are <{}InitializedDeviceBE>] 
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:879) 
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:755) 
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:732) 
    at org.springframework.batch.item.xml.StaxEventItemReader.doRead(StaxEventItemReader.java:240) 
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88) 

,但如果我刪除的命名空間是這樣的:<DevicesGetInitializedByDatesResult><DevicesGetInitializedByDatesResult xmlns="">,作業將成功運行。 我該如何解決這個問題? 非常感謝

源XML:

<?xml version="1.0" encoding="utf-8"?> 
<DevicesGetInitializedByDatesResult xmlns="http://schemas.alcatel.com/iptv/singtel"> 
    <InitializedDeviceBE> 
    <AccountExternalID>23064787</AccountExternalID> 
    <DeviceExternalID>596377703</DeviceExternalID> 
    <IPTVDeviceID>90fa277b-cc9e-4906-81b8-43f6c5c9e312</IPTVDeviceID> 
    <ManufactureName>CISCO</ManufactureName> 
    <ModelNo>ISB2200</ModelNo> 
    <InitializedDate>2015-09-02T16:28:38.5400000+08:00</InitializedDate> 
    <SerialNo>174871919</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
    <AccountExternalID>23274415</AccountExternalID> 
    <DeviceExternalID>595867836</DeviceExternalID> 
    <IPTVDeviceID>78445f81-4a33-4803-83b1-4ec7a58200a6</IPTVDeviceID> 
    <ManufactureName>MOTOROLA</ManufactureName> 
    <ModelNo>VIP1200AP</ModelNo> 
    <InitializedDate>2015-09-02T16:43:10.8900000+08:00</InitializedDate> 
    <SerialNo>M91005FBA791</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
    <AccountExternalID>28847933</AccountExternalID> 
    <DeviceExternalID>596369404</DeviceExternalID> 
    <IPTVDeviceID>a8c37fc0-31dc-4c2a-aea6-6a5242d415fd</IPTVDeviceID> 
    <ManufactureName>MOTOROLA</ManufactureName> 
    <ModelNo>VIP1200AP</ModelNo> 
    <InitializedDate>2015-09-02T16:29:06.9770000+08:00</InitializedDate> 
    <SerialNo>M91024FAN201</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
    <AccountExternalID>23064787</AccountExternalID> 
    <DeviceExternalID>596377704</DeviceExternalID> 
    <IPTVDeviceID>05c05595-42b9-404c-a088-ee9a65f9b948</IPTVDeviceID> 
    <ManufactureName>MOTOROLA</ManufactureName> 
    <ModelNo>VIP1200AP</ModelNo> 
    <InitializedDate>2015-09-02T16:29:07.7900000+08:00</InitializedDate> 
    <SerialNo>M90950FEF793</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
    <AccountExternalID>22063076</AccountExternalID> 
    <DeviceExternalID>595907213</DeviceExternalID> 
    <IPTVDeviceID>e017d823-c56a-4822-98fe-fc9417eb7aef</IPTVDeviceID> 
    <ManufactureName>MOTOROLA</ManufactureName> 
    <ModelNo>VIP1200AP</ModelNo> 
    <InitializedDate>2015-09-02T16:23:39.1700000+08:00</InitializedDate> 
    <SerialNo>M90928FB3431</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
    <AccountExternalID>25219018</AccountExternalID> 
    <DeviceExternalID>593796598</DeviceExternalID> 
    <IPTVDeviceID>071e5276-2dd6-4ea0-bcdd-ec68e2e3b217</IPTVDeviceID> 
    <ManufactureName>MOTOROLA</ManufactureName> 
    <ModelNo>VIP1208AP</ModelNo> 
    <InitializedDate>2015-09-02T16:56:36.9330000+08:00</InitializedDate> 
    <SerialNo>M90843FB8568</SerialNo> 
    <PreviousSerialNo /> 
    </InitializedDeviceBE> 
</DevicesGetInitializedByDatesResult> 

型號代碼:

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 
import javax.xml.bind.annotation.XmlType; 


@XmlRootElement(name = "InitializedDeviceBE") 
@XmlType(propOrder = { "accountExternalID", "deviceExternalID", "iPTVDeviceID", "manufactureName", "modelNo", "initializedDate", "serialNo", "previousSerialNo"}) 
public class SmaUpdstbparamsRecord{ 

    private String accountExternalID; 
    private String deviceExternalID; 
    private String iPTVDeviceID; 
    private String manufactureName; 
    private String modelNo; 
    private String initializedDate; 
    private String serialNo; 
    private String previousSerialNo; 
    private String recordParty; 

    @XmlElement(name = "AccountExternalID", required = true) 
    public String getAccountExternalID() { 
     return accountExternalID; 
    } 

    public void setAccountExternalID(String accountExternalID) { 
     this.accountExternalID = accountExternalID; 
    } 

    @XmlElement(name = "DeviceExternalID" , required = true) 
    public String getDeviceExternalID() { 
     return deviceExternalID; 
    } 
    public void setDeviceExternalID(String deviceExternalID) { 
     this.deviceExternalID = deviceExternalID; 
    } 

    @XmlElement(name = "IPTVDeviceID" , required = true) 
    public String getiPTVDeviceID() { 
     return iPTVDeviceID; 
    } 
    public void setiPTVDeviceID(String iPTVDeviceID) { 
     this.iPTVDeviceID = iPTVDeviceID; 
    } 

    @XmlElement(name = "ManufactureName" , required = true) 
    public String getManufactureName() { 
     return manufactureName; 
    } 
    public void setManufactureName(String manufactureName) { 
     this.manufactureName = manufactureName; 
    } 

    @XmlElement(name = "ModelNo", required = true) 
    public String getModelNo() { 
     return modelNo; 
    } 
    public void setModelNo(String modelNo) { 
     this.modelNo = modelNo; 
    } 

    @XmlElement(name = "InitializedDate", required = true) 
    public String getInitializedDate() { 
     return initializedDate; 
    } 
    public void setInitializedDate(String initializedDate) { 
     this.initializedDate = initializedDate; 
    } 

    @XmlElement(name = "SerialNo", required = true) 
    public String getSerialNo() { 
     return serialNo; 
    } 
    public void setSerialNo(String serialNo) { 
     this.serialNo = serialNo; 
    } 

    @XmlElement(name = "PreviousSerialNo", required = true) 
    public String getPreviousSerialNo() { 
     return previousSerialNo; 
    } 
    public void setPreviousSerialNo(String previousSerialNo) { 
     this.previousSerialNo = previousSerialNo; 
    } 

    @XmlTransient 
    public String getRecordParty() { 
     return recordParty; 
    } 

    public void setRecordParty(String recordParty) { 
     this.recordParty = recordParty; 
    } 

    @Override 
    public String toString() { 
     return "SmaUpdstbparamsRecord [accountExternalID=" + accountExternalID + ", deviceExternalID=" 
       + deviceExternalID + ", iPTVDeviceID=" + iPTVDeviceID + ", manufactureName=" + manufactureName 
       + ", modelNo=" + modelNo + ", initializedDate=" + initializedDate + ", serialNo=" + serialNo 
       + ", previousSerialNo=" + previousSerialNo + "]"; 
    } 




} 

春天批次:

<bean id="sma-updstbparams-reader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step"> 
     <property name="resource" value="file:#{jobExecutionContext['input.file']}" /> 
     <!-- <property name="linesToSkip" value="2" /> --> 
     <property name="unmarshaller" ref="reportUnmarshaller" /> 
     <property name="fragmentRootElementName" value="InitializedDeviceBE" /> 
    </bean> 
    <bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="classesToBeBound"> 
      <list> 
      <value>com.ncs.sma.model.SmaUpdstbparamsRecord</value> 
      </list> 
     </property> 
    </bean> 

預期的XML文件:

剛剛拆分源文件,像下面的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<DevicesGetInitializedByDatesResult xmlns="http://schemas.alcatel.com/iptv/singtel"> 
    <InitializedDeviceBE> 
     <AccountExternalID>23064787</AccountExternalID> 
     <DeviceExternalID>596377703</DeviceExternalID> 
     <IPTVDeviceID>90fa277b-cc9e-4906-81b8-43f6c5c9e312</IPTVDeviceID> 
     <ManufactureName>CISCO</ManufactureName> 
     <ModelNo>ISB2200</ModelNo> 
     <InitializedDate>2015-09-02T16:28:38.5400000+08:00</InitializedDate> 
     <SerialNo>174871919</SerialNo> 
     <PreviousSerialNo/> 
    </InitializedDeviceBE> 
    <InitializedDeviceBE> 
     <AccountExternalID>23064787</AccountExternalID> 
     <DeviceExternalID>596377704</DeviceExternalID> 
     <IPTVDeviceID>05c05595-42b9-404c-a088-ee9a65f9b948</IPTVDeviceID> 
     <ManufactureName>MOTOROLA</ManufactureName> 
     <ModelNo>VIP1200AP</ModelNo> 
     <InitializedDate>2015-09-02T16:29:07.7900000+08:00</InitializedDate> 
     <SerialNo>M90950FEF793</SerialNo> 
     <PreviousSerialNo/> 
    </InitializedDeviceBE> 
</DevicesGetInitializedByDatesResult> 

回答

0

正如我所遇到的問題,有兩種方法來解決,

一個是打造「package-info.java」,只是像這樣:All child elements are tagged with namespace [Jaxb2Marshaller]

的第二種方式是使用XStreamMarshaller代替Jaxb2Marshaller的代碼是:

<bean id="tradeMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> 
    <property name="autodetectAnnotations" value="true"/> 
    <property name="aliases"> 
    <map> 
     <entry key="InitializedDeviceBE" value="com.ncs.sma.model.SmaUpdstbparamsRecord" /> 
     </map> 
    </property> 
</bean> 

第二種方式更簡單,方便,配置更少。