Question

I have simple XML file.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <DataTypes>
   <Type name="type_integer" type="integer" />
   <Type name="valid_string" type="string" />
</DataTypes>

I've created two classes to map this file to these classes. First of them should correspond DataTypes tag. I defined this class as follows:

@XmlRootElement(name = "DataTypes")
    public class DataTypes {

       private List<DataType> types;

       public List<DataType> getTypes() {
         return types;
       }

       public void setTypes(List<DataType> types) {
         this.types = types;
       }

}

The second class correspond to Type tag. I defined this class as follows:

@XmlAccessorType(XmlAccessType.FIELD)
public class DataType {

@XmlAttribute
private String name;

@XmlAttribute
private String type;

public String getName() {
    return name;
}

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

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}
}

When I'm loading data the collections of types is null. I know that I should use @XmlElementWrapper annotation for collection in the first class, but how it should look like? Is this annotation should look like @XmlElementWrapper(name = "DataTypes")? But in this case this name will be the same as root element? How I should do this correcly?

Thanks in advance for help

Was it helpful?

Solution

With your current mapping JAXB is expecting the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DataTypes>
   <types name="type_integer" type="integer" />
   <types name="valid_string" type="string" />
</DataTypes>

You just need to do the following in the DataTypes class to specify the Type element should be used instead of the default types which was derived from the property name.

   @XmlElement(name="Type")
   public List<DataType> getTypes() {
     return types;
   }

Additional information can be found on my blog:


About @XmlElementWrapper

@XmlElementWrapper is used to add a grouping element around elements representing items in a collection. If you would like to learn more about what it can do check out the following on my blog:

OTHER TIPS

With Blaises modification you are doing perfectly well. So with it you can parse your xml and get your Type - List.

@XmlElement(name="Type")
public List<DataType> getTypes() {
  return types;
}

A XmlElementWrapper element generates a classless tag. So you could write

@XmlElementWrapper(name = "TypesCol")
@XmlElement(name="Type")
public List<DataType> getTypes() {
  return types;
}

and you could process an XML like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <DataTypes>
   <TypesCol>
     <Type name="type_integer" type="integer" />
     <Type name="valid_string" type="string" />
   </TypesCol>
</DataTypes>

without having a class for TypesCol. But you need a reference to the root class DataTypes. Therefore a wrapper for DataTypes is not a valid option here.

the easy way is to create an xsd file, you add all you elements or types in it, then you can generate the class files.

To generate you will need jaxb.jar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top