I have the following XML structure that I am parsing into the classes mentioned below:
<?xml version="1.0" encoding="utf-16"?>
<OrderDiscount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Status>Failed</Status>
<Errors>Error : Customer 00000037 not found : Product 51 D0003 not found</Errors>
<DeliveryID>00000037</DeliveryID>
<CarriageAmount>0</CarriageAmount>
<TotalDiscount>0</TotalDiscount>
<OrderLines>
<Warehouse>51</Warehouse>
<Product>D0003</Product>
<ContractPrice>N</ContractPrice>
<UnitOfSale>20</UnitOfSale>
<Ordered>1</Ordered>
<ListPrice>10</ListPrice>
<NetP开发者_开发技巧rice>10</NetPrice>
<NewNetPrice>0</NewNetPrice>
</OrderLines>
</OrderDiscount>
Here, we can have multiple instances of <OrderLines>
. I am trying to parse it using the following 2 classes
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* @author nka
*
*/
@XStreamAlias("OrderDiscount")
public class OrderDiscount {
@XStreamAlias("Status")
private String Status;
@XStreamAlias("Errors")
private String Errors;
@XStreamAlias("DeliveryID")
private String DeliveryID;
@XStreamAlias("CarriageAmount")
private String CarriageAmount;
@XStreamAlias("TotalDiscount")
private String TotalDiscount;
@XStreamAlias("OrderLines")
private List<OrderLines> OrderLines;
}
2nd Class:
public class OrderLines {
@XStreamAlias("Warehouse")
private String Warehouse;
@XStreamAlias("Product")
private String Product;
@XStreamAlias("ContractPrice")
private String ContractPrice;
@XStreamAlias("UnitOfSale")
private String UnitOfSale;
@XStreamAlias("Ordered")
private String Ordered;
@XStreamAlias("ListPrice")
private String ListPrice;
@XStreamAlias("NetPrice")
private String NetPrice;
@XStreamAlias("NewNetPrice")
private String NewNetPrice;
}
I am using the following code to parse it:
OrderDiscount orderDiscount = null;
final XStream xstream = new XStream(new StaxDriver());
xstream.processAnnotations(OrderDiscount.class);
xstream.processAnnotations(OrderLines.class);
orderDiscount = (OrderDiscount) xstream.fromXML(response);
I am getting the following error:
Target exception: com.thoughtworks.xstream.converters.ConversionException: Warehouse : Warehouse : Warehouse : Warehouse
---- Debugging information ----
message : Warehouse : Warehouse
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : Warehouse : Warehouse
class : uk.co.portaltech.quicklive.thirdparty.datel.OrderDiscount
required-type : java.util.ArrayList
path : /OrderDiscount/OrderLines/Warehouse
line number : 1
-------------------------------
Can someone please help me to correct the structure of my Java OBJECTS to resolve this?
You can use custom handler for ur XML parsing in more generic way so that it can parse all the pojo classes depending on the annotations
精彩评论