【问题标题】:jaxb unmarshalling - unexpected element exceptionjaxb 解组 - 意外的元素异常
【发布时间】:2016-02-26 22:15:48
【问题描述】:

我尝试将 XML 文件解组为对象。

我收到了这个错误:

javax.xml.bind.UnmarshalException: unexpected element (URI:"", lokal:"ORDER"). Expected elements are <{http://www.opentrans.org/XMLSchema/1.0}ACCOUNT>,<{http://www.opentrans.org/XMLSchema/1.0}ACCOUNTING_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}ADDRESS>,<{http://www.opentrans.org/XMLSchema/1.0}AGREEMENT>,<{http://www.opentrans.org/XMLSchema/1.0}ARTICLE_ID>,<{http://www.opentrans.org/XMLSchema/1.0}ARTICLE_PRICE>,<{http://www.opentrans.org/XMLSchema/1.0}BUYER_AID>,<{http://www.opentrans.org/XMLSchema/1.0}BUYER_PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}CARD>,<{http://www.opentrans.org/XMLSchema/1.0}CASH>,<{http://www.opentrans.org/XMLSchema/1.0}CATALOG_REFERENCE>,<{http://www.opentrans.org/XMLSchema/1.0}CHECK>,<{http://www.opentrans.org/XMLSchema/1.0}CONTACT>,<{http://www.opentrans.org/XMLSchema/1.0}CONTROL_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}COST_CATEGORY_ID>,<{http://www.opentrans.org/XMLSchema/1.0}DEBIT>,<{http://www.opentrans.org/XMLSchema/1.0}DELIVERY_DATE>,<{http://www.opentrans.org/XMLSchema/1.0}DELIVERY_PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}FINAL_DELIVERY_PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}INTERNATIONAL_AID>,<{http://www.opentrans.org/XMLSchema/1.0}INTERNATIONAL_RESTRICTIONS>,<{http://www.opentrans.org/XMLSchema/1.0}INVOICE_PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}MANUFACTURER_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}MIME>,<{http://www.opentrans.org/XMLSchema/1.0}MIME_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_HEADER>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_ITEM>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_ITEM_LIST>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_PARTIES>,<{http://www.opentrans.org/XMLSchema/1.0}ORDER_SUMMARY>,<{http://www.opentrans.org/XMLSchema/1.0}PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}PARTY_ID>,<{http://www.opentrans.org/XMLSchema/1.0}PAYMENT>,<{http://www.opentrans.org/XMLSchema/1.0}PAYMENT_TERM>,<{http://www.opentrans.org/XMLSchema/1.0}PHONE>,<{http://www.opentrans.org/XMLSchema/1.0}PRICE_FLAG>,<{http://www.opentrans.org/XMLSchema/1.0}PUBLIC_KEY>,<{http://www.opentrans.org/XMLSchema/1.0}REMARK>,<{http://www.opentrans.org/XMLSchema/1.0}SHIPMENT_PARTIES>,<{http://www.opentrans.org/XMLSchema/1.0}SOURCING_INFO>,<{http://www.opentrans.org/XMLSchema/1.0}SPECIAL_TREATMENT_CLASS>,<{http://www.opentrans.org/XMLSchema/1.0}SUPPLIER_PARTY>,<{http://www.opentrans.org/XMLSchema/1.0}TRANSPORT>,<{http://www.opentrans.org/XMLSchema/1.0}TRANSPORT_PARTY>

我的解组过程如下:

InputStream is = new FileInputStream(xmlfile);
JAXBContext jc = JAXBContext.newInstance( ORDER.class );
Unmarshaller u = jc.createUnmarshaller();
ORDER order = (ORDER) u.unmarshal(is);

我的 XML 实体看起来像:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "orderheader",
    "orderitemlist",
    "ordersummary"
})
@XmlRootElement(name = "ORDER")
public class ORDER {

    @XmlElement(name = "ORDER_HEADER", required = true)
    protected ORDERHEADER orderheader;
    @XmlElement(name = "ORDER_ITEM_LIST", required = true)
    protected ORDERITEMLIST orderitemlist;
    @XmlElement(name = "ORDER_SUMMARY", required = true)
    protected ORDERSUMMARY ordersummary;
    @XmlAttribute(name = "version")
    protected String version;
    @XmlAttribute(name = "type")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String type;

    getters and setters
}

我的 XMLFile 看起来像:

<?xml version="1.0" encoding="UTF-8" ?>
<ORDER version="1.0" type="standard">
    <ORDER_HEADER>
        ...
    </ORDER_HEADER>
    <ORDER_ITEM_LIST>
        <ORDER_ITEM>
            ...
        </ORDER_ITEM>
    </ORDER_ITEM_LIST>
    <ORDER_SUMMARY>
        ...
    </ORDER_SUMMARY>
</ORDER>

那么我的解组过程有什么问题? XML 实体是用 xjc 创建的。

我也尝试使用简单的 xml 文件/对象。这对我来说很好。

【问题讨论】:

    标签: java xml jaxb unmarshalling


    【解决方案1】:

    错误信息基本上归结为:

    javax.xml.bind.UnmarshalException:
      Unexpected element: <{}ORDER>
      Expected element: <{http://www.opentrans.org/XMLSchema/1.0}ORDER>
    

    基本上,错误是您的架构适用于命名空间http://www.opentrans.org/XMLSchema/1.0,但您的文档没有命名空间。

    要使其发挥作用,您有 3 个选择:

    1. 为 XML 数据添加命名空间
    2. 从 XML 架构中删除命名空间
    3. 不支持命名空间的解析

    如果可能,我建议 #1:

    <?xml version="1.0" encoding="UTF-8" ?>
    <ORDER version="1.0" type="standard" xmlns="http://www.opentrans.org/XMLSchema/1.0">
        <ORDER_HEADER>
            ...
        </ORDER_HEADER>
        <ORDER_ITEM_LIST>
            <ORDER_ITEM>
                ...
            </ORDER_ITEM>
        </ORDER_ITEM_LIST>
        <ORDER_SUMMARY>
            ...
        </ORDER_SUMMARY>
    </ORDER>
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 2019-05-15
      • 2023-04-02
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多