Advisor
Advisor

Reputation: 21

Jaxb how to suppress a tagName but get the value

Using JAXB I am generating xml.The XML must look like

<batch>
<elem id=101>
<field name=country>US</field>
<field name=criteria>Test criteria</field>
:
:
</elem>
</batch>

all internal contents are fields. I have java class called 'field' which has 'name' and 'value' string properties with getters and setters. 'Elem' class has 'field' as arraylist. Using Jaxb when i marshal it contains "value" also inside field. What is the efficient way to achieve this? Should i filter 'value' tag(suppress) or the java object structure should be changed? What I am getting is this

<batch>
<elem id=101>
<field name=country><value>US</value></field>
<field name=criteria><value>Test criteria</value></field>
:
:
</elem>
</batch>

The field class looks like below

import javax.xml.bind.annotation.XmlAttribute;
public class Field {
    private String name;
    private String value;
    public Field() {
    }
    public Field(String name, String value) {
        super();
        this.name = name;
        this.value = value;
    }
    @XmlAttribute
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

Upvotes: 1

Views: 345

Answers (1)

Advisor
Advisor

Reputation: 21

I have figured it out that its a tag @XmlValue which should be applied to value field in the Field class and that gives me proper formatted xml.

Upvotes: 1

Related Questions