当前位置: 首页 > 知识库问答 >
问题:

Jaxb解封错误:意外元素(URI:“”,本地:“ProcessedSalesOrderTypeList”)。预期的元素为

盖玉石
2023-03-14

我试图解封XML文件。我创建了Jaxb类,但是当我试图解封时,它会给出:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"processedSalesOrderTypeList"). Expected elements are <{api.supplieroasis.com}processedSalesOrderMessage>,<{api.supplieroasis.com}salesOrderMessage>,<{api.supplieroasis.com}shipperOfRecordAccountNumber>,<{api.supplieroasis.com}shippingAccountNumber>,<{api.supplieroasis.com}uspsMailerId>,<{api.supplieroasis.com}warehouseName>

这是我的ObjectFactory.java类:

@XmlRegistry
public class ObjectFactory {

    private final static QName _ProcessedSalesOrderMessage_QNAME = new QName("api.supplieroasis.com", "processedSalesOrderMessage");
    private final static QName _WarehouseName_QNAME = new QName("api.supplieroasis.com", "warehouseName");
    private final static QName _ShippingAccountNumber_QNAME = new QName("api.supplieroasis.com", "shippingAccountNumber");
    private final static QName _ShipperOfRecordAccountNumber_QNAME = new QName("api.supplieroasis.com", "shipperOfRecordAccountNumber");
    private final static QName _SalesOrderMessage_QNAME = new QName("api.supplieroasis.com", "salesOrderMessage");
    private final static QName _UspsMailerId_QNAME = new QName("api.supplieroasis.com", "uspsMailerId");
}
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="api.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="salesChannelOrderNumber" type="xs:string"/>
  <xs:element name="salesChannelName" type="xs:string"/>
  <xs:element name="orderDate" type="xs:dateTime"/>
  <xs:element name="sofsCreatedDate" type="xs:dateTime"/>
  <xs:element name="warehouseName">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="code"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="shipToAddress">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="contactName"/>
        <xs:element type="xs:string" name="address1"/>
        <xs:element type="xs:string" name="city"/>
        <xs:element type="xs:string" name="stateOrProvince"/>
        <xs:element type="xs:int" name="postalCode"/>
        <xs:element type="xs:string" name="countryCode"/>
        <xs:element type="xs:string" name="phone"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="returnAddress">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="contactName"/>
        <xs:element type="xs:string" name="address1"/>
        <xs:element type="xs:string" name="city"/>
        <xs:element type="xs:string" name="stateOrProvince"/>
        <xs:element type="xs:int" name="postalCode"/>
        <xs:element type="xs:string" name="countryCode"/>
        <xs:element type="xs:string" name="phone"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="shippingSpecifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="isThirdPartyBilling"/>
        <xs:element type="xs:string" name="isSignatureRequired"/>
        <xs:element type="xs:string" name="isDeclaredValueRequired"/>
        <xs:element name="smallParcelShipment">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="shippingServiceLevel">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="code"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element type="xs:string" name="isExport"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="branding" type="xs:string"/>
  <xs:element name="orderFulfillment" type="xs:string"/>
  <xs:element name="orderId" type="xs:int"/>
  <xs:element name="status" type="xs:string"/>
  <xs:element name="retailChannelCode" type="xs:string"/>
  <xs:element name="retailerOrderCode" type="xs:string"/>
  <xs:element name="retailOrderNumber" type="xs:int"/>
  <xs:element name="actionRequired" type="xs:string"/>
  <xs:element name="processedSalesOrderLine">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:byte" name="salesChannelLineId"/>
        <xs:element type="xs:byte" name="salesChannelLineNumber"/>
        <xs:element type="xs:string" name="partnerSKU"/>
        <xs:element type="xs:string" name="barcode"/>
        <xs:element type="xs:string" name="salesChannelSKU"/>
        <xs:element type="xs:byte" name="quantity"/>
        <xs:element type="xs:float" name="itemPrice"/>
        <xs:element type="xs:int" name="lineId"/>
        <xs:element type="xs:int" name="itemId"/>
        <xs:element type="xs:string" name="itemName"/>
        <xs:element type="xs:string" name="lineStatus"/>
        <xs:element type="xs:float" name="unitCost"/>
        <xs:element type="xs:string" name="unitCostCurrencyCode"/>
        <xs:element type="xs:float" name="firstCost" minOccurs="0"/>
        <xs:element type="xs:string" name="firstCostCurrencyCode" minOccurs="0"/>
        <xs:element type="xs:float" name="additionalShippingCost" minOccurs="0"/>
        <xs:element type="xs:string" name="additionalShippingCostCurrencyCode" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<processedSalesOrderTypeList xmlns:ns2="api.example.com">
    <totalCount>4</totalCount>
    <list>
        <ns2:salesChannelOrderNumber>17456-1</ns2:salesChannelOrderNumber>
        <ns2:salesChannelName>OSTK</ns2:salesChannelName>
        <ns2:orderDate>2017-02-09T13:25:59.000-07:00</ns2:orderDate>
        <ns2:sofsCreatedDate>2017-02-09T09:40:19.000-07:00</ns2:sofsCreatedDate>
        <ns2:warehouseName>
            <ns2:code>Avia Warehouse</ns2:code>
        </ns2:warehouseName>
        <ns2:shipToAddress>
            <ns2:contactName>TestOrder1</ns2:contactName>
            <ns2:address1>5961 Adam Street</ns2:address1>
            <ns2:city>Salt Lake City</ns2:city>
            <ns2:stateOrProvince>UT</ns2:stateOrProvince>
            <ns2:postalCode>84047</ns2:postalCode>
            <ns2:countryCode>US</ns2:countryCode>
            <ns2:phone>801-555-5961</ns2:phone>
        </ns2:shipToAddress>
        <ns2:returnAddress>
            <ns2:contactName>Joe User-1355</ns2:contactName>
            <ns2:address1>1355 Adam Street</ns2:address1>
            <ns2:city>Salt Lake City</ns2:city>
            <ns2:stateOrProvince>UT</ns2:stateOrProvince>
            <ns2:postalCode>84047</ns2:postalCode>
            <ns2:countryCode>US</ns2:countryCode>
            <ns2:phone>801-555-1355</ns2:phone>
        </ns2:returnAddress>
        <ns2:shippingSpecifications>
            <ns2:isThirdPartyBilling>false</ns2:isThirdPartyBilling>
            <ns2:isSignatureRequired>false</ns2:isSignatureRequired>
            <ns2:isDeclaredValueRequired>false</ns2:isDeclaredValueRequired>
            <ns2:smallParcelShipment>
                <ns2:shippingServiceLevel>
                    <ns2:code>GROUND</ns2:code>
                </ns2:shippingServiceLevel>
            </ns2:smallParcelShipment>
            <ns2:isExport>false</ns2:isExport>
        </ns2:shippingSpecifications>
        <ns2:branding/>
        <ns2:orderFulfillment>ALLOW_PARTIAL_FILL</ns2:orderFulfillment>
        <ns2:orderId>50048</ns2:orderId>
        <ns2:status>PROCESSING</ns2:status>
        <ns2:retailChannelCode>OSTK_1716</ns2:retailChannelCode>
        <ns2:retailerOrderCode>OrderCode-123459790</ns2:retailerOrderCode>
        <ns2:retailOrderNumber>123459790</ns2:retailOrderNumber>
        <ns2:actionRequired>true</ns2:actionRequired>
        <ns2:processedSalesOrderLine>
            <ns2:salesChannelLineId>1</ns2:salesChannelLineId>
            <ns2:salesChannelLineNumber>1</ns2:salesChannelLineNumber>
            <ns2:partnerSKU>GPI42829404</ns2:partnerSKU>
            <ns2:barcode>GPI42829404</ns2:barcode>
            <ns2:salesChannelSKU>GPI42829404</ns2:salesChannelSKU>
            <ns2:quantity>1</ns2:quantity>
            <ns2:itemPrice>81.98</ns2:itemPrice>
            <ns2:lineId>58838</ns2:lineId>
            <ns2:itemId>217412</ns2:itemId>
            <ns2:itemName>Altimair TPU Camping Airmat with Built-in Foot Pump</ns2:itemName>
            <ns2:lineStatus>PROCESSING</ns2:lineStatus>
            <ns2:unitCost>0.00</ns2:unitCost>
            <ns2:unitCostCurrencyCode>USD</ns2:unitCostCurrencyCode>
            <ns2:firstCost>41.60</ns2:firstCost>
            <ns2:firstCostCurrencyCode>USD</ns2:firstCostCurrencyCode>
            <ns2:additionalShippingCost>0.00</ns2:additionalShippingCost>
            <ns2:additionalShippingCostCurrencyCode>USD</ns2:additionalShippingCostCurrencyCode>
        </ns2:processedSalesOrderLine>
    </list>
</processedSalesOrderTypeList>

你能请帮助,因为我被卡住了,不能提供我的客户开发这个功能的延迟原因。

谢谢,Jayesh Jain

共有1个答案

许天逸
2023-03-14

我可以想到两个不同的原因,为什么您的解编组程序失败。

  1. 在架构中,没有定义“ProcessedSalesOrderTypeList”。在您提到的错误中,有效的元素是:

<{api.supplieroasis.com}processedsalesOrdermessage>,<{api.supplieroasis.com}salesordermessage>,<{api.supplieroasis.com}shipperofrecordaccountnumber>,<{api.supplieroasis.com}shippingaccountnumber>,<{api.supplieroasis.com}uspsmailerid>,<{api.supplieroasis.com}warehousename>

XML命名空间包含在大括号{}中,元素名称紧随其后。这些值在ObjectFactory中指定。正如您所看到的,由于ObjectFactory中没有定义“ProcessedSalesOrderTypeList”,所以当解封程序在XML中遇到“ProcessedSalesOrderTypeList”元素时,它不知道如何处理它。

我在xjc生成的代码中遇到过名称空间不匹配的问题,这导致在反序列化过程中许多元素返回null。奇怪的是,有些元素会被正确解析,但有些却不能。首先,我将添加一个ValidationEventHandler来查看所有XML验证错误:

setEventHandler(new DefaultValidationEventHandler());

unexpected element (uri:"", local:"MyEntity")

一旦定位到类,请查看名称空间是否正确匹配。我遇到过一个问题,命名空间是空白的,需要填充。

用与XML相同的命名空间填充命名空间。代码示例:

<test:MyEntity xmlns:test="com.example">
...
</test:MyEntity>

@xmlelement(name=“myentity”,namespace=“test:com.example”)

@XmlElementDecl(namespace = "test:com.example", name = "MyEntity")
public JAXBElement<MyEntity> createMyEntity(MyEntity value) {
    return new JAXBElement<MyEntity>(_MyEntity_QNAME, MyEntity.class, null, value);
}

在名称空间全部匹配之后,反序列化就可以工作了。如果没有,返回并检查异常并发布它,我将提供一些更多的见解。

 类似资料: