jaxb unmarshalling - 意外的元素异常

时间:2016-02-26 22:15:49

标签: java xml jaxb unmarshalling

我尝试将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文件/对象。这对我来说很好。

1 个答案:

答案 0 :(得分:0)

错误消息基本归结为:

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. 没有命名空间支持的解析
  4. 我建议#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>