本文介绍了解组XML文件时如何忽略命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当源文件的命名空间为:

when the source file have the namespace with:

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 => ,作业将成功运行。
我该如何解决这个问题?
非常感谢

but if I remove the namespace like this:<DevicesGetInitializedByDatesResult> or <DevicesGetInitializedByDatesResult xmlns="">, the job will run successfully.how can I fix this problem?Thanks a lot

源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文件:

just split the source file, like the below xml file:

<?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>


推荐答案

正如我遇到的问题,有两种方法可以解决,

As the problem I encountered, Two ways to fix,

一个是创建package-info.java,就像这样:

One is create "package-info.java", just like this: All child elements are tagged with namespace [Jaxb2Marshaller]

第二种方式使用 XStreamMarshaller 代替Jaxb2Marshaller,代码是:

The second way is use the XStreamMarshaller instead of the Jaxb2Marshaller the code is:

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

第二种方式更简单,更方便,配置更少。

The second way is more simple,convenience and less configuration.

这篇关于解组XML文件时如何忽略命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:07