Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse <foo value1="a" value2="b">value3</foo> with JAXB?

Tags:

xml

jaxb

A substring of my XML looks like this:

<foo value1="a" value2="b">value3</foo>

I'm trying to parse this using JAXB. I've managed to parse the values value1 and value2 but I'm having problems with the "root" value since it doesn't have any tag associated with it.

My class:

@XmlType(propOrder = {"value3"}, name = "foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo
{
    @XmlAttribute
    private String value1;

    @XmlAttribute
    private String value2;

    @XmlElement(name = "")
    private String value3;
}

Any ideas?

like image 649
Jonas Avatar asked Jun 15 '11 09:06

Jonas


1 Answers

You can use the @XmlValue annotation:

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo
{
    @XmlAttribute
    private String value1;

    @XmlAttribute
    private String value2;

    @XmlValue
    private String value3;
}
like image 101
bdoughan Avatar answered Oct 29 '22 15:10

bdoughan