SK176H
SK176H

Reputation: 1319

JAXB - How to specify xml attributes in binding Java Class

I know to create a JAXB Class to marshal/unmarshall an xml like this

<outertag>  
    <innerelement>
        <innerElementDetail1>some value</inner-element-detail1>
    </innerelement>
</outertag> 

here is the class I created

@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {

    @XmlElement(name = "innerelement")
    private List<InnerElement> innerElemements

    public static InnerElement{
        private String innerElementDetail;
        // getters and setters
    }
}

If I have to have an attribute on one of the inner elements like this

<outertag>  
    <innerelement attribute1="attribute1value">
        <innerElementDetail1>some value</inner-element-detail1>
    </innerelement>
</outertag> 

how do I do that ?

Upvotes: 0

Views: 95

Answers (1)

Nyamiou The Galeanthrope
Nyamiou The Galeanthrope

Reputation: 1214

This should do it :

@XmlRootElement(name ="outertag")
@XmlAccessorType(XmlAccessType.FIELD)
public class OuterTag {

    @XmlElement(name = "innerelement")
    private List<InnerElement> innerElemements

    public static InnerElement{
        @XmlAttribute(name = "attribute1")
        protected String attribute1;

        private String innerElementDetail;
        // getters and setters
    }
}

Upvotes: 1

Related Questions