简体   繁体   English

jaxb解组-意外的元素异常

[英]jaxb unmarshalling - unexpected element exception

I try to unmarshalling an XML File to an Object. 我尝试将XML文件解组到对象。

I got this error: 我收到此错误:

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>

My unmarshelling process looks like: 我的解壳过程如下:

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

My XML Entity looks like: 我的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
}

My XMLFile looks like: 我的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>

So what is wrong with my unmarshalling process? 那么我的编组过程有什么问题呢? The XML Entitys are created with xjc. XML实体是使用xjc创建的。

I tried it also with simple xml files/objects. 我也尝试了简单的xml文件/对象。 This works fine for me. 这对我来说很好。

The error message basically boils down to: 该错误消息基本上可以归结为:

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

Basically, the error is that your schema is for namespace http://www.opentrans.org/XMLSchema/1.0 , but your document doesn't have a namespace. 基本上,错误是您的架构用于名称空间http://www.opentrans.org/XMLSchema/1.0 ,但是您的文档没有名称空间。

To make it work, you have 3 choices: 要使其正常工作,您有3种选择:

  1. Add namespace to XML data 将名称空间添加到XML数据
  2. Remove namespace from XML schema 从XML模式中删除名称空间
  3. Parse without namespace support 没有名称空间支持的解析

I'd suggest #1, if possible: 我建议#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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM