i have a XML parsed pojo like below
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"activity",
"orderDetails"
})
@XmlRootElement(name = "GxML")
public class GxML {
@XmlElement(name = "Activity", required = true)
public String activity;
@XmlElement(name = "OrderDetails", required = true)
public GxML.OrderDetails orderDetails;
//get set
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"initiatorRole",
"productDetails"
})
public static class OrderDetails {
@XmlElement(name = "InitiatorRole", required = true)
public String initiatorRole;
//get set
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"product"
})
public static class ProductDetails {
@XmlElement(name = "Product", required = true)
public List<GxML.OrderDetails.ProductDetails.Product> product;
public List<GxML.OrderDetails.ProductDetails.Product> getProduct() {
if (product == null) {
product = new ArrayList<GxML.OrderDetails.ProductDetails.Product>();
}
return this.product;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"quantity",
"commodity"
})
public static class Product {
@XmlElement(name = "Quantity", required = true)
public BigDecimal quantity;
@XmlElement(name = "Commodity", required = true)
public String commodity;
//get set
}}}}
i need to Get All Field Names and their corresponding values using Java Reflection
Class c1 = gXml.getClass();
Field[] valueObjFields = c1.getDeclaredFields()[1].getType().getDeclaredFields();
for (int i = 0; i < valueObjFields.length; i++) {
String fieldName = valueObjFields[i].getName();
valueObjFields[i].setAccessible(true);
Object newObj = valueObjFields[i].get(gXml.getOrderDetails());
orderMap.put(valueObjFields[i].getAnnotation(XmlElement.class).name(), newObj);
if("productDetails".equalsIgnoreCase(fieldName)){
Class c2 = gXml.getOrderDetails().getProductDetails().getProduct().get(0).getClass();
Field[] valueObjFields1 = c2.getDeclaredFields();
for (int j = 0; j < valueObjFields1.length; j++) {
String fieldName2 = valueObjFields1[j].getName();
valueObjFields1[j].setAccessible(true);
Object newObj2 = valueObjFields1[j].get(gXml.getOrderDetails().getProductDetails().getProduct());
orderMap.put(valueObjFields1[j].getAnnotation(XmlElement.class).name(), newObj2);
} } }
for order details it is working fine but in product details to get quantity and commodity it is giving illegal argument exception at
Object newObj2 = valueObjFields1[j].get(gXml.getOrderDetails().getProductDetails().getProduct());
how to get list values using reflection.
Aucun commentaire:
Enregistrer un commentaire