0

Currently I have a number of different xml elements wrappers in a class.

I want to add a single attribute to the xml tags for the xml elements. This will work as a flag for my application.

Every xml element will have a different attribute value so I thought to pack them together.

For this reason I wrote a new object that has two fields. A generic value field and a string field to use it as the attribute.

Unfortunatly I cannot find a way to extract the value from the value field without the presens of "garbage" xml tag.

Is there any way to do this. To be more clear I present the specific parts of the code.

@XmlRootElement(name = "client")
class Client {
    private List<String> names;

    private List<Integer> salaries;

    private List<Long> socialSecurityNos;

    @XmlElementWrapper(name = "names")
    @XmlElement(name = "name")
    public List<String> getNames() {
        return this.names;
    }

    @XmlElementWrapper(name = "salaries")
    @XmlElement(name = "salary")
    public List<String> getSalaries() {
        return this.salaries;
    }

    @XmlElementWrapper(name = "socialsecuritynos")
    @XmlElement(name = "socialsecurityno")
    public List<String> getSocialSecurityNo() {
        return this.socialSecurityNos;
    }
    ...
    ...
}

This produces the following xml

<foo>
    <names>
        <name>
            George
        </name>
        <name>
            John
        </name>
    </names>
    <salaries>
        <salaries>
        ...
        ...
        </salaries>
    </salaries>
    <socialSecurityNo>
        <socialSecurityNo>
        ...
        ...
        </socialSecurityNo>
    </socialSecurityNo>
</foo>

The new Value,attribute pair class that I wrote.

@XmlRootElement(name = "client")
class GenericElement <T> {
    private String attribute;

    private T value;

    public T getValue() {
        return this.value;
    }

    @XmlAttribute(name = "flag")
    public String getAttribute() {
        return this.attribute;
    }

}

And of course i changed the Lists types

@XmlRootElement(name = "client")
class Client {
    private List<GenericElement<String>> names;

    private List<GenericElement<Integer>> salaries;

    private List<GenericElement<Long>> socialSecurityNos;

    ...
    ...

I want to get this result.

<foo>
    <names>
        <name flag="on">
            George
        </name>
        ...
        <name flag="off">
            John
        </name>
    </names>

    ....
    .....
        </socialSecurityNo>
    </socialSecurityNo>
</foo>

Instead I get that, with the "garbage" value tag.

<foo>
    <names>
        <name flag="on">
            <value>George</value>
        </name>
        ...
        <name flag="off">
            <value>Value</value>
        </name>
    </names>

    ....
    ....    
    ....................</value>
        </socialSecurityNo>
    </socialSecurityNo>
</foo>
Chrys
  • 3
  • 4

1 Answers1

0

It should be possible to get rid of the extra value tags by annotating the getter for the value field with @XmlValue. There's a blog post by the MOXy lead developer Blaise Doughan that gives a nice overview of how it works.

So you should try this:

@XmlRootElement(name = "client")
class GenericElement <T> {
    ...
    @XmlValue
    public T getValue() {
        return this.value;
    }
    ...
}
Hein Blöd
  • 1,553
  • 1
  • 18
  • 25
  • Apparently XmlValue works only with with a "known" type otherwise a NullPointerException is thrown during marshalling. – Chrys May 02 '16 at 15:12
  • Ah I see, it doesn't work with generic types. In that case, [this](http://stackoverflow.com/questions/8807296/jaxb-generic-xmlvalue/8901997#8901997) other answer should do the trick. – Hein Blöd May 02 '16 at 21:53