Dave4988
Dave4988

Reputation: 705

Jaxb2Marshaller unmarshalling in spring - unexpected element

I'm pretty new to unmarshalling using Jaxb, but I think I've got everything configured right yet I'm getting this error on runtime:

 javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xml.product.place.com/prod/alerts/v_1_0", local:"Record"). Expected elements are (none)

I'm unmarshalling a pretty complex XML file using POJOs generated by JaxB based off of a few XSDs.

Here's the relevant part of the XML:

<PlaceAlerts xmlns="http://xml.product.place.com/prod/alerts/v_1_0"">
<Information>
    <ReportType>EIFK</ReportType>
    <Number>401ZC00155</Number>
    <ReportDate>2005-02-05</ReportDate>
</Information>
<Record>
    <id>123</id>
    <cid><![CDATA[12327*312457495]]></ccid>
...
</PlaceAlerts>

The XML file goes on, but the error comes on the Record opening tag.

Here's Record.java:

package com.etc.etc.etc;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "cid",
    "alerts"
})
@XmlRootElement(name = "Record")
public class Record {

    @XmlElement(required = true)
    protected Object id;
    @XmlElement(required = true)
    protected Object cid;
    @XmlElement(name = "Alerts", required = true)
    protected Alerts alerts;

    public Object getId() {
        return id;
    }

    public void setId(Object value) {
        this.id = value;
    }
    public Object getCid() {
        return cid;
    }
    public void setCid(Object value) {
        this.cid = value;
    }
    public Alerts getAlerts() {
        return alerts;
    }
    public void setAlerts(Alerts value) {
        this.alerts = value;
    }  
}

And the bean declaration:

<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

    <property name="packagesToScan">
        <list>
            <value>com.etc.etc.etc.*</value>
        </list>
    </property>
</bean>

Any thoughts? If you need to see anything else let me know.

Upvotes: 2

Views: 4143

Answers (2)

In order to use "packagesToScan", I believe package-info.java class should present in your java package where you have generated the classes from Schema (XSD)

Upvotes: 0

Dave4988
Dave4988

Reputation: 705

I figured out the problem, but not the root cause of the problem. I changed the "packagesToScan" property to the "classesToBeBound" property. For some reason, I don't think the classes were getting picked up by the package pattern matcher. Here's what I changed it to:

<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.etc.etc.etc.Alert</value>
            <value>com.etc.etc.etc.Channel</value>
            etc...
        </list>
    </property>
</bean>

If anyone has any insight as to why packagesToScan didn't work as I expected, I'd love to hear it.

Upvotes: 1

Related Questions