How to wrap a collection in one parent tag

I have a class called XmlSource that contains a collection of objects of type XmlSourceAudioLang, and the XmlSourceAudioLang class has a field called the language that I need to marshal XML, i.e.

@XmlAccessorType(XmlAccessType.FIELD) public class XmlSource { @XmlElement(name="original_audio_language", nillable=true) protected Set<XmlSourceAudioLang> originalAudio; } 

and

 @XmlAccessorType(XmlAccessType.FIELD) public class XmlSourceAudioLang { @XmlElement(nillable = true) private String language; } 

With this setting, JAXB generates an original_audio_language tag for each object in the collection:

 <original_audio_language> <language>Ukrainian</language> </original_audio_language> <original_audio_language> <language>Russian</language> </original_audio_language> 

Is there a way to configure the binding so that there is only one parent tag original_audio_language?

+4
source share
1 answer

Assuming you want you to like this:

 <original_audio_language> <language>Ukrainian</language> <language>Russian</language> </original_audio_language> 

Then try the following:

 @XmlAccessorType(XmlAccessType.FIELD) public class XmlSource { @XmlElementWrapper(name="original_audio_language") @XmlElement(name="language") protected Set<XmlSourceAudioLang> originalAudio; } 

 @XmlAccessorType(XmlAccessType.FIELD) public class XmlSourceAudioLang { @XmlValue private String language; } 
+9
source

Source: https://habr.com/ru/post/1389157/


All Articles