Using Jaxb to Unmarshal/Marshal a List<String>

Using JAXB to unmarshal/marshal a ListString

I used @LiorH's example and expanded it to:



@XmlRootElement(name="List")
public class JaxbList<T>{
protected List<T> list;

public JaxbList(){}

public JaxbList(List<T> list){
this.list=list;
}

@XmlElement(name="Item")
public List<T> getList(){
return list;
}
}

Note, that it uses generics so you can use it with other classes than String. Now, the application code is simply:



@GET
@Path("/test2")
public JaxbList test2(){
List list=new Vector();
list.add("a");
list.add("b");
return new JaxbList(list);
}

Why doesn't this simple class exist in the JAXB package? Anyone see anything like it elsewhere?

Using JAXB to unmarshal/marshal a ListString - Inheritance

Annotations are obtained through reflection. The annotation is bound to the type it's defined in.

public class AnnotationTest {

public static class A
{
@XmlElement(name="item")
public void doIt() { }
}

public static class B extends A
{
@XmlElement(name="name")
public void doIt() { }
}

public static void main(String[] args)
{
B b = new B();
Method m = b.getClass().getMethods()[0];
Annotation[] ann = m.getDeclaredAnnotations();
System.out.println( ann.length ); // prints 1
}
}

So this behavior is not due to the reflection mechanism itself. It seems like the JAXB framework does explicitly list the annotation on the method in the class and the super classes. Surprisingly this still works, but not as you would like.

Can't you just remove @XmlElement(name="item") in the base class?

EDIT

According to your comment (but still not sure if I understand all correctly), I would suggest:

@XmlRootElement(name="list")
public class JaxbBaseList<T> implements JaxbList<T>{
protected List<T> list;

public List<T> getList(){
return list;
}
}

public class JaxbPrimitiveList<T> extends JaxbList<T>{
protected List<T> list;

@XmlElement( name="item" )
public List<T> getList(){
return list;
}
}

@XmlSeeAlso( Uri )
public class JaxbUriList<Uri> extends JaxbList<Uri>{
protected List<Uri> list;

@XmlElement( name="uri" )
public List<Uri> getList(){
return list;
}
}

JAXB Marshall/Unmarshall a class object with List variable member

You need a public getter for your sides list, and the setter needs to be named sides. Something like:

public List<Integer> getSides() {
return side;
}
public void setSides(List<Integer> sides) {
this.side = sides;
}

should do it. Might also be worth renaming your side field to sides for the sake of consistency.

JAXB Unmarshal a list of objects

Your annotations need to go on the property (get or set method) instead of the field. If you wish to have them on the field (instance variable), the you need to annotate your class with @XmlAccessorType(XmlAccessType.FIELD), like you have on your Year class.

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

UPDATE

Instead of specifying the namespace on @XmlRootElement you will need to leverage a package level @XmlSchema annotation (on a class called package-info) to match the namespace qualification.

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

Unmarshalling nested list of xml items using JAXB

You will need to define a custom XmlAdapter. The complicated part in your case is that you want to map one XML element into multiple Java Element objects. This means that, in Java., your XmlAdapter needs to be configured for collection of Element objects. Assuming your example XML fragment is part of a document:

<document>
<elements>
<element>
....
</element>
<elements>
</document>

Then you will need to configure the XmlAdapter for the List<Element> field in the Java Document class:

class Document {
@XmlJavaTypeAdapter(CustomAdapter.class)
List<Element> elements;
}

Then you your CustomAdapter class can receive a list of Element objects (corresponding to the actual XML structure with the nested items) and produce a list of Element with the structure you want.

For an example, check JAXB XmlAdapter – Customized Marshaling and Unmarshaling



Related Topics



Leave a reply



Submit