Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb UnMarshal Error : unexpected element (uri:"", local:"processedSalesOrderTypeList"). Expected elements are

I am trying to unmarshal the XML file.I created the Jaxb classes but when i try to unmarshal,it gives me :

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>

This is my ObjectFactory.java class :

@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");
}

XSD:

<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 file to parse:

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

Can you please help as i am stuck and not able to provide my Client reason for the delay in developing this functionality.

Thanks, Jayesh Jain

like image 268
Jayesh Avatar asked Mar 22 '17 06:03

Jayesh


2 Answers

I can think of 2 different reasons why your Unmarshaller is failing.

  1. Looking in your schema, "processedSalesOrderTypeList" is not defined. In the error you mentioned, the valid 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>

The XML namespace is contained within the braces {} and the element name comes right after it. These values are specified in your ObjectFactory. As you can see, since "processedSalesOrderTypeList" is not defined within your ObjectFactory so when the Unmarshaller encounters the "processedSalesOrderTypeList" element in your XML, it has no idea how to process it.

  1. I've had an issue with the namespaces mismatching in code generated from xjc which caused a lot of the elements to return null during deserialization. The strange thing was some of the elements would be correctly parsed but some wouldn't. First I would add a ValidationEventHandler to see all XML validation errors:

    unmarshaller.setEventHandler(new DefaultValidationEventHandler());

Then, if you get an error similar to the one below, this answer should help you.

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

I would look for the class which has this field (it could be a root-level class). Use judgement in finding this class, if the element tag it is failing to parse is the root element then most likely the class has the same exact name as the element. If the element is a child of another element, look in the respective class for that particular element.

Once you locate the class, look to see that the namespaces match up correctly. I've had an issue where the namespace was blank and needed to be filled in.

Fill in the namespace with the same namespace that the XML has. Code Example:

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

@XmlElement(name = "MyEntity", namespace = "test:com.example")

In essence, make sure the namespace in the annotation and the XML are matching, if not and namespace verification is enabled it will refuse to unmarshal the XML.

If this does not fix your problem and you see an exception regarding ObjectFactory, I would look for the QName that corresponds to this particular element. The correct snippet of code should something like this, making sure the namespaces are matching (create it if it does not exist):

QName _MyEntity_QNAME = new QName("test:com.example", "MyEntity");

In the same ObjectFactory, look for the @XmlElementDecl. This is the annotation that defines how the XML element is converted into a Java object. The correct code should look something like this:

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

After the namespaces all match up, deserialization should work. If not, go back and check for the exception and post it and I will provide some more insight.

like image 94
jon5477 Avatar answered Sep 28 '22 02:09

jon5477


Your JAXB object factory and XSD seems to be different..try recreating the JAXB classes again.

like image 44
saraboji sowndarajan Avatar answered Sep 28 '22 02:09

saraboji sowndarajan