简体   繁体   中英

“unexpected element” while trying to map XML to POJOs

I am trying to map the below XML to POJOs using JAXB so that I can use the data in the XML, however, I'm getting the error below:

! javax.xml.bind.UnmarshalException: unexpected element 
(uri:"http://webservices.amazon.com/AWSECommerceService/2011-08-01", 
local:"ItemSearchResponse"). Expected elements are <{}ItemSearchResponse>

XML:

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
    <Items>
        <Item>
            <ASIN>B001DJLCRC</ASIN>
            <DetailPageURL>
                http://www.amazon.com/Breaking-Bad-Complete-First-Season/dp/B001DJLCRC%3FSubscriptionId%3DAKIAJ6JZ43XIWIUIIQLA%26tag%3Dsample026-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001DJLCRC
            </DetailPageURL>
            <ItemLinks>
                <ItemLink>
                    <Description>Technical Details</Description>
                    <URL>
                        http://www.amazon.com/Breaking-Bad-Complete-First-Season/dp/tech-data/B001DJLCRC%3FSubscriptionId%3DAKIAJ6JZ43XIWIUIIQLA%26tag%3Dsample026-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB001DJLCRC
                    </URL>
                </ItemLink>
            </ItemLinks>
            <ItemAttributes>
                <Actor>Bryan Cranston</Actor>
                <Actor>Aaron Paul</Actor>
                <Manufacturer>Sony Pictures Home Entertainment</Manufacturer>
                <ProductGroup>DVD</ProductGroup>
                <Title>Breaking Bad: The Complete First Season</Title>
            </ItemAttributes>
        </Item>
    </Items>
</ItemSearchResponse>

My POJOs (getter/setters are skipped from question on purpose)

ItemSearchResponse

@XmlRootElement(name="ItemSearchResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemSearchResponse
{
    @XmlElement(name="Items")
    private Items items = null;
}

Items

@XmlAccessorType(XmlAccessType.FIELD)
public class Items {
    @XmlElement(name="Item")
    List<Item> items = new ArrayList();
}

Item

@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
    @XmlElement(name="ASIN")
    private String asin;
    @XmlElement(name="ItemAttributes")
    private ItemAttributes attributes;
}

Item Attributes

@XmlAccessorType(XmlAccessType.FIELD)
public class ItemAttributes {
    @XmlElement(name="Title")
    private String title;
    @XmlElement(name="Author")
    private String author;
}

questions

  • How can I resolve the error? Are my POJOs not setup correct? If so, how should I re-structure the POJOs?

  • There are multiple Author in the xml. How can I map them to an Array or list of sort.

You need to use the package level @XmlSchema annotation to map the namespace qualification for your model.

package-info.java

@XmlSchema( 
    namespace = "http://www.example.org/package", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

For More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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