How to annotate a list using @XmlElement?

前端 未结 3 1425
忘掉有多难
忘掉有多难 2020-12-29 03:08

I have the following annotation using javax.xml.bind.annotation.XmlElement:

@XmlElement         
public List getKeywords() {
    r         


        
相关标签:
3条回答
  • 2020-12-29 03:52

    Above answer by - Blaise Doughan is completely correct

    Another simple way is , even if you don't write the - @XmlElementWrapper

     private List<String> keywords;
    
     @XmlElementWrapper
        @XmlElement(name="keyword")
        public List<String> getKeywords() {
            return keywords;
        }
    

    You can use it this way - write the XmlAccessorType on Class level , then XML element name will be same as the class member name - keywords

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Content {
    
        private List<String> keywords;
    
        public Content() {}
    
    
        public List<String> getKeywords() {
            return keywords;
        }
    
        public void setKeywords(List<String> keywords) {
            this.keywords = keywords;
        }  
    
    }
    
    0 讨论(0)
  • 2020-12-29 03:55

    Use this form:

    @XmlElementWrapper(name="keywords")
    @XmlElement(name="keyword")
    

    Please note that if keywords is empty then you will get <keywords />.

    Sometimes you will need to add @XmlRootElement to your class (depends on the context) and the @XmlAccessorType(XmlAccessType.?) annotation. I usually use @XmlAccessorType(XmlAccessType.FIELD) and annotate my fields with @XmlElement.

    0 讨论(0)
  • 2020-12-29 04:02

    You need to leverage @XmlElementWrapper and @XmlElement.

    Java Model

    Content

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Content {
    
        private List<String> keywords;
    
        public Content() {}
    
        @XmlElementWrapper
        @XmlElement(name="keyword")
        public List<String> getKeywords() {
            return keywords;
        }
    
        public void setKeywords(List<String> keywords) {
            this.keywords = keywords;
        }  
    
    }
    

    Demo Code

    Demo

    import java.util.*;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Content.class);
    
            List<String> strings = new ArrayList<String>(2);
            strings.add("foo");
            strings.add("bar");
    
            Content content = new Content();
            content.setKeywords(strings);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(content, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <content>
        <keywords>
            <keyword>foo</keyword>
            <keyword>bar</keyword>
        </keywords>
    </content>
    

    For More Information

    Below are links to a couple articles from my blog that provide additional information:

    • http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
    • http://blog.bdoughan.com/2012/12/jaxb-representing-null-and-empty.html
    0 讨论(0)
提交回复
热议问题