user1479589
user1479589

Reputation: 315

JAXB Key Value problrm

So what I am looking to do is to achieve this structure:

<root>
   <child>value</child>
   <child>value</child>
   .
   .
</root>

The problem is I do not know how many children are there in advances, so need a list. I have tried this but I end up with:

<root>
   <child/>
   <child/>
   .
   .
</root>

This is using JAXB.

Please help me out..

Upvotes: 1

Views: 123

Answers (2)

user1479589
user1479589

Reputation: 315

Okey so What I did was added a new annotation and setter and getter methods and it worked eg:

@XmlValue protected String myval;

            public String getMyval(){
                return this.myval;
            }

            public void setMyval(String myval){
                this.myval = myval;
            }

Upvotes: 0

bdoughan
bdoughan

Reputation: 149007

You could do one the following:

Option #1

Root

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    List<String> child;

}

Option #2

Root

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    List<Child> child;

}

Child

@XmlAccessorType(XmlAccessType.FIELD)
public class Child {
    @XmlValue
    String value;

}

For More Information

Upvotes: 2

Related Questions