java - JAXB Unmarshall same elements with different attributes (lang) -


i have rest xml feed following usage of language differentiation

<name xml:lang="cs">letní 2001/2002</name> <name xml:lang="en">summer 2001/2002</name> 

the lang attribute occurs multiple different elements, other name. there way me unmarshall 1 of elements based on selected language? or list or better map of both of them?

i know possibly creating different class each of elements, don't want have fifty classes because language choice, each resource.

edit: have not yet considered moxy, have if can't done jaxb alone.

note: i'm eclipselink jaxb (moxy) lead , member of jaxb (jsr-222) expert group.

moxy allows map element based on value of xml attribute using @xmlpath extension:

java model (foo)

import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.xmlpath;  @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class foo {      @xmlpath("name[@xml:lang='cs']/text()")     private string csname;      @xmlpath("name[@xml:lang='en']/text()")     private string enname;  } 

demo

import java.io.file; import javax.xml.bind.*;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(foo.class);          unmarshaller unmarshaller = jc.createunmarshaller();         file xml = new file("src/forum17731167/input.xml");         foo foo = (foo) unmarshaller.unmarshal(xml);          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.marshal(foo, system.out);     }  } 

for more information


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -