Tomasz Lebkichler
Tomasz Lebkichler

Reputation: 1

JAXB XML to object serializing

I have this XML structure:

<diagnosedSystemTypes>
   <string>system_1</string>   
   <string>system_2</string>
   <string>system_3</string>
   <string>system_4</string>
</diagnosedSystemTypes>

What im trying to do is, to deserialize this elements to DiagnosedSystemTypes list.

This is how looks my DiagnosedSystemType class.

@XmlRootElement( name = "string" )
public class DiagnosedSystemType {

    String name; 

    @XmlElement (name = "string")
    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }
}

While this diagnosedSystemTypes are nested in Server tag, i put this fields in Server class.

@XmlElementWrapper(name = "diagnosedSystemTypes")
@XmlElement (name = "string")
List<DiagnosedSystemType> diagnosedSystemTypes = null;

Now during the deserialization, i got proper list of diagnosedSystemTypes, but the string element inside, are null's :( It's strange, because the number of elements inside the list are ok (4). Any ideas what i'm doing wrong?

Upvotes: 0

Views: 53

Answers (1)

Zielu
Zielu

Reputation: 8562

@XmlRootElement( name = "diagnosedSystemTypes" )
public class DiagnosedSystemType {

    @XmlElement (name = "string")
    List<String> names; 


    public List<String> getNames(){
        if (names == null) names = new ArrayList<>();
        return name;
    }
}

Your diagnosedSystemTypes contains list of Strings not just one name.

Upvotes: 1

Related Questions