Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:37:06+00:00 2026-05-29T15:37:06+00:00

i have the following classes which is marshalled as an XML <?xml version=1.0 encoding=UTF-8

  • 0

i have the following classes which is marshalled as an XML

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customer id="100">
        <age>21</age>
        <hobbies>
            <hobby>
        <cost>sd</cost>
        <name>na</name>
            </hobby>
            <hobby>
                <cost>sd</cost>
                <name>nb</name>
            </hobby>
        </hobbies>
        <name>test</name>
    </customer>     

However, when i tried to unmarshall, I can only create the customer object but not hobby, which returns null
Am i doing something wrong here? The problem seems to be with the XML hierarchy?

package com.mytest.jxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@XmlRootElement(name="customer")
@XmlSeeAlso({Hobby.class})
public class Customer {

    String name;
    int age;
    int id;
    @XmlElementRef
    private List<Hobby> hobbyList = new ArrayList<Hobby>();

    public Customer(){
        //hobbyList = new ArrayList<Hobby>();
    }

    //@XmlElement
    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    //@XmlElement
    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    //@XmlAttribute
    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
    public void addHobby(Hobby h) {
        hobbyList.add(h);
    }
    @XmlElementWrapper(name="hobbies")
    @XmlElement(name = "hobby")
    public List<Hobby> getHobbies(){
        return hobbyList;
    }
}

package com.mytest.jxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
class Hobby {

    private String name;
    private String cost;

    public Hobby(){
    }

    public Hobby(String name, String cost){
        this.name = name;
        this.cost = cost;
    }
    //@XmlElement
    public void setName(){
        this.name = name;
    }
    @XmlElement
    public String getName(){
        return name;
    }
    //@XmlElement
    public void setCost(){
        this.cost = cost;
    }
    @XmlElement
    public String getCost(){
        return cost;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:37:07+00:00Added an answer on May 29, 2026 at 3:37 pm

    Having addHobby(Hobby h) is not enough for JAXB. Your class should be real POJO and should have void setHobbies(List<Hobby> hobbyList) { this.hobbyList = hobbyList; }. Also I think you can safely remove @XmlElementRef from hobbyList field.

    EDIT: I have checked your classes and created the version which works OK:

    package test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementRef;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.apache.commons.lang.builder.ToStringStyle;
    
    @XmlRootElement(name = "customer")
    public class Customer {
    
        String name;
        int age;
        int id;
    
        private List<Hobby> hobbyList = new ArrayList<Hobby>();
    
        public Customer() {
        }
    
        @XmlElement
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @XmlElement
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @XmlAttribute
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void addHobby(Hobby h) {
            hobbyList.add(h);
        }
    
        @XmlElementWrapper(name = "hobbies")
        @XmlElement(name = "hobby")
        public List<Hobby> getHobbies() {
            return hobbyList;
        }
    
        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }
    }
    
    package test;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.apache.commons.lang.builder.ToStringStyle;
    
    @XmlRootElement
    class Hobby {
    
        private String name;
        private String cost;
    
        public Hobby() {
        }
    
        public Hobby(String name, String cost) {
            this.name = name;
            this.cost = cost;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @XmlElement
        public String getName() {
            return name;
        }
    
        public void setCost(String cost) {
            this.cost = cost;
        }
    
        @XmlElement
        public String getCost() {
            return cost;
        }
    
        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project which it's domain contain following classes: Courier Customer Food Order
I have the following 3 rails classes, which are all stored in one table,
In my project I have the following three interfaces, which are implemented by classes
I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following classes: public class Person { public String FirstName { set;
I have the following classes in my models file class HardwareNode(models.Model): ip_address = models.CharField(max_length=15)
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.