使用简单 xml 2.1.0 将 xml 映射到 java
Issue mapping xml to java using simplexml 2.1.0
我正在尝试使用 SimpleXML 2.1.0 将 Web 服务给出的 xml 响应映射到 java 对象,但我卡住了。
这是xml:
<QueryINVAP-WS-ASSETResponse baseLanguage="ES" creationDateTime="2016-11-08T17:10:09-03:00">
<INVAP-WS-ASSETSet>
<ASSET rowstamp="265381811">
<ASSETID>1529</ASSETID>
<ASSETNUM>2503</ASSETNUM>
<DESCRIPTION>POWER CHASSIS()</DESCRIPTION>
<ITEMNUM>A000232</ITEMNUM>
<LOCATION>LOCATIONTEST</LOCATION>
<SERIALNUM>123456789</SERIALNUM>
<SITEID>TVD</SITEID>
</ASSET>
</INVAP-WS-ASSETSet>
如您所想,我想转换为 java 对象的唯一数据是 assetid、assetnum、description、itemnum、location、SerialNum 和 siteid。
到目前为止,这是我拥有的:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict = false)
public class Activos {
@ElementList(inline = true, entry = "ASSET", type = Activo.class)
private List<Activo> activos;
@Root
class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
}
我得到的错误是:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=ASSET, inline=true, name=, required=true, type=class invap.invapgestionmovil.modelos.Activos$Activo) on field 'activos' private java.util.List invap.invapgestionmovil.modelos.Activos.activos for class invap.invapgestionmovil.modelos.Activos at line 1
我做错了什么?
好吧,我终于解决了这个问题。我创建了三个 类:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict=false)
public class QueryINVAPWSASSETResponse {
@Element(name = "INVAP-WS-ASSETSet")
private INVAPWSASSETSet set;
}
@Root(name = "INVAP-WS-ASSETSet", strict=false)
public class INVAPWSASSETSet {
@ElementList(inline=true, name = "ASSET")
private List<Activo> activos;
}
@Root(name = "ASSET", strict = false)
public class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
现在,当我打电话时,我得到了资产 "wrapped" 和其他两个 类。
希望这对其他人有帮助:)
此致
我正在尝试使用 SimpleXML 2.1.0 将 Web 服务给出的 xml 响应映射到 java 对象,但我卡住了。
这是xml:
<QueryINVAP-WS-ASSETResponse baseLanguage="ES" creationDateTime="2016-11-08T17:10:09-03:00">
<INVAP-WS-ASSETSet>
<ASSET rowstamp="265381811">
<ASSETID>1529</ASSETID>
<ASSETNUM>2503</ASSETNUM>
<DESCRIPTION>POWER CHASSIS()</DESCRIPTION>
<ITEMNUM>A000232</ITEMNUM>
<LOCATION>LOCATIONTEST</LOCATION>
<SERIALNUM>123456789</SERIALNUM>
<SITEID>TVD</SITEID>
</ASSET>
</INVAP-WS-ASSETSet>
如您所想,我想转换为 java 对象的唯一数据是 assetid、assetnum、description、itemnum、location、SerialNum 和 siteid。
到目前为止,这是我拥有的:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict = false)
public class Activos {
@ElementList(inline = true, entry = "ASSET", type = Activo.class)
private List<Activo> activos;
@Root
class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
}
我得到的错误是:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=ASSET, inline=true, name=, required=true, type=class invap.invapgestionmovil.modelos.Activos$Activo) on field 'activos' private java.util.List invap.invapgestionmovil.modelos.Activos.activos for class invap.invapgestionmovil.modelos.Activos at line 1
我做错了什么?
好吧,我终于解决了这个问题。我创建了三个 类:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict=false)
public class QueryINVAPWSASSETResponse {
@Element(name = "INVAP-WS-ASSETSet")
private INVAPWSASSETSet set;
}
@Root(name = "INVAP-WS-ASSETSet", strict=false)
public class INVAPWSASSETSet {
@ElementList(inline=true, name = "ASSET")
private List<Activo> activos;
}
@Root(name = "ASSET", strict = false)
public class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
现在,当我打电话时,我得到了资产 "wrapped" 和其他两个 类。
希望这对其他人有帮助:) 此致