Damian Fox
Damian Fox

Reputation: 37

JAXB Integer and Long parsing

I have an object structure that contains multiple numeric values:

@XmlTransient
public class Person
{
private Integer age;
private Long shoeSize;
public setAge(Integer age){...}
public getAge(){return age;}
...
}

public class Child extends Person
{
private Integer readingAge;
public setReadingAge(Integer readingAge){...}
public getReadingAge(){return readingAge;}
}

public class Team
{
private ArrayList<Child> children = new Arraylist<Child>();
...
}

So when I marshall the Child object the age is missed. There is a possability that age can be null so i cannot just add it as an int value.

The reasoning behind the @xmlTransient is due to i need the marshalling to marshall the whole document in alphabetical order. If i remove the transient the person elements are ordered then the child elements are added to the end.

So my questions are: Is there anyway i can get the age recognised without it defaulting the value to 0? Can I order the document as a whole without setting the transient flag? I have tried to simplfy my example as the real example has 100's of elements

Target XML:

<Team>
    <Child>
        <age>5</age>
        <readingAge>25</readingAge>
        <shoeSize>1</shoeSize>
    </Child>
</Team>

But if age was null:

<Team>
    <Child>
        <readingAge>25</readingAge>
        <shoeSize>1</shoeSize>
    </Child>
<Team>

Marshalling class:

Child c = new Child(5, 9, 25);
Team t  = new Team();
t.getChildren().put(c);
if (getMarshaller() == null)
         {
            JAXBContext context = JAXBContext.newInstance(Team.class);
            setMarshaller(context.createMarshaller());
         }

         StringWriter sw = new StringWriter();
         getMarshaller().marshal(t, sw);
         String xmlString = sw.toString();

Upvotes: 0

Views: 463

Answers (1)

laune
laune

Reputation: 31290

So when I marshall the Child object the age is missed. There is a possability that age can be null so i cannot just add it as an int value.

Correct, and that is the way to handle it if a null age should be marshalled and (later) unmarshalled as null again. The absence of an element is the way a null value is represented.

Can I order the document as a whole without setting the transient flag?

Yes, you can: the ArrayList<Child> children in class Team contains an ordered collection of Child objects, and they will be marshalled in this order, and (later) unmarshalled back again into the same list.

I need the marshalling to marshall the whole document in alphabetical order.

This is not something JAXB will do for you automatically. If, for instance, the children list should be ordered alphabetically, you need to sort it, e.g., by calling Collections::sort with a Team object's field children as its (first) parameter. (I can't provide an example since I don't see a String field, either in Person or in Child, not even in the XML example.)

Edit

I only need the ordering where inheritance is concerned. E.g. <age> <readingAge> <shoeSize>

Again, this is not something JAXB will do, and it is a very unusual requirement. (What is the purpose?) Probably your best option would be to override the Person getters in the Child class and define the ordering by annotating the class with

@XmlType(propOrder = {"age","readingAge","shoeSize"})

Upvotes: 1

Related Questions