Reputation: 41
I'm trying to deserialize an XML string to an object that has a list inside. The deserialization is partially successful. These are my java classes.
public class Vector {
public double x;
public double y;
public double z;
}
public class TP{
public Date Time;
public double thrust;
public double isp;
public double duration = .5;
public Vector direction;
}
public class SOP{
public Date Time;
public Vector Position;
public Vector Velocity;
public Vector Acceleration;
public double mass;
}
@XmlRootElement(name="OPS")
@XmlAccessorType(XmlAccessType.FIELD)
public class OPS{
public OPS() {
TPS= new ArrayList<TP>();
}
public SOP initialState;
public List<TP> TPS;
public Date Time;
}
This is the XML
<?xml version="1.0" encoding="utf-8"?>
<OPS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<initialState>
<Time>2021-03-18T09:55:07.1259143-04:00</Time>
<Position>
<x>0</x>
<y>0</y>
<z>0</z>
</Position>
<Velocity>
<x>0</x>
<y>0</y>
<z>0</z>
</Velocity>
<Acceleration>
<x>0</x>
<y>0</y>
<z>0</z>
</Acceleration>
<mass>0</mass>
</initialState>
<TPS>
<TP>
<Time>2021-03-18T09:55:07.1119203-04:00</Time>
<thrust>1</thrust>
<isp>1</isp>
<duration>0.5</duration>
<direction>
<x>0</x>
<y>0</y>
<z>0</z>
</direction>
</TP>
<TP>
<Time>2021-03-18T09:55:07.1259143-04:00</Time>
<thrust>1</thrust>
<isp>1</isp>
<duration>0.5</duration>
<direction>
<x>0</x>
<y>0</y>
<z>0</z>
</direction>
</TP>
<TP>
<Time>2021-03-18T09:55:07.1259143-04:00</Time>
<thrust>1</thrust>
<isp>1</isp>
<duration>0.5</duration>
<direction>
<x>0</x>
<y>0</y>
<z>0</z>
</direction>
</TP>
</TPS>
<Time>2021-03-18T09:55:07.1259143-04:00</Time>
</OPS
After deserializing, the OPS object will have data on the Time and initialState properties, but the TPS list is null. What am I missing? Your help will be really appreciated.
Upvotes: 1
Views: 70
Reputation: 841
On the TPS property add @XmlElementWrapper and @XmlElement annotations:
@XmlRootElement(name="OPS")
@XmlAccessorType(XmlAccessType.FIELD)
public class OPS {
public OPS() {
TPS= new ArrayList<TP>();
}
public SOP initialState;
@XmlElementWrapper(name="TPS")
@XmlElement(name="TP")
public List<TP> TPS;
public Date Time;
More can find details here: JAXB & Collection Properties
Upvotes: 1