Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a collection in a single parent tag

I have a class called XmlSource which contains a collection of objects of type XmlSourceAudioLang, and the XmlSourceAudioLang class has a field called language which I need to marshal to 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 setup, JAXB generates a 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 original_audio_language tag ?

like image 488
Fabien Coppens Avatar asked Jan 18 '23 21:01

Fabien Coppens


1 Answers

Assuming that you mean that you want this:

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

Then try this:

@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;    
}
like image 88
skaffman Avatar answered Feb 01 '23 10:02

skaffman