2

I cant unmarshall xml because don't understand how to annotate object class in the another object. Please help.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<ODZ xmlns="http://www.company.com/1.0" >
    <Data DataID="ZZZ">
        <UserData UserKey="user_001">
                 <UserEvent>...</UserEvent>
            </UserData> 
     </Data>
</ODZ>

Container classes: I. First level with link to the second (ODZ -> Data).

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "ODZ", namespace = "http://www.company.com/1.0")
public class ODZContainer {

    private ImportContainer importContainer;

    @XmlElement (name = "Data", type=ImportContainer.class)
    public ImportContainer getImportContainer() {
        return importContainer;
    }
}

II. Second level with link to the third level(Data -> UserData).

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "Data")
public class ImportContainer {

    private String DataID;
    private ArrayList<UserDataBean> userDataBean;

    @XmlElement (name = "UserData", type=UserDataBean.class)
    public ArrayList<UserDataBean> getUserDataBean() {
        return userDataBean;
    }

    @XmlAttribute(name = "DataID")
    public String getDataID() {
        return DataID;
    }
}

III. Third level with link to the fourth level(UserData-> UserEvent).

@XmlAccessorType(XmlAccessType.NONE)
    @XmlRootElement(name = "UserData")
    public class UserDataBean {
        private ArrayList<UserEventBean> userEventData;
        private String userEventID;

        @XmlAttribute(name = "UserKey")
        public String getUserEventID() {
            return userEventID;
        }

        @XmlElement (name = "UserEvent", type=UserEventBean.class)
        public ArrayList<UserEventBean> getUserEventBean() {
            return userEventData;
        }
    }

1 Answer 1

1

The namespace qualification in your JAXB metadata does not match your XML. You can use the package level @XmlSchema annotation to specify the namespace qualification for your model.

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

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

For More Information on JAXB and Namespaces


Notes About Your Metadata

Since the type of the ArrayList is already specified, you don't need to specify it via the @XmlElement annotation. It doesn't hurt, but its not necessary.

@XmlElement (name = "UserData", type=UserDataBean.class)
public ArrayList<UserDataBean> getUserDataBean() {
    return userDataBean;
}

@XmlAccessorType(XmlAccessType.NONE) means that nothing is mapped unless it is explicitly annotated. This may or not be what you want. You may find the following article useful:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment. Please answer for one question. Is that corrent using: @XmlElement (name = "Data", type=ImportContainer.class) in the parent class and @XmlRootElement(name = "Data") public class ImportContainer {.. in the child?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.