I'm trying to dynamically unmarshall xml files and therefore needs to alter the @XmlRootElement and @XmlElement annotations when instantiating my model.
I've seen some examples of how to do this, but I'm unclear of how to implement them and whether they would be the best approach. E.g. Modify a class definition's annotation string parameter at runtime
//Update I need to unmarshall feeds of products dynamically, they will be unmarshalled according to mapping specified by the user, which will be used by the models. One of the pieces that I do not know how to change dynamically is the xmlElements annotation. If I can modify this dynamically then the rest will work.
Users will complete a product mapping model for each product feed they wish to use. Each product feed will be unmarshalled using the mapping model, the mapping model has a set criteria, e.g. name, url which the user can map to. This will then be used to populate the Products model when unmarshalled.
Typical flow:
UnmarshallAll method
Get all productfeeds (contains details or xml url, and mapping id)
For each productfeed - get the mapping model from the id of the product feed and set the url of the xml file. Call an XmlUnmarshallingmethod with the url and the productFeedMapping.
In the unmarshalling method, I then need to generate the Products model ready for unmarshalling, but the annotations for unmarshalling need to change with data from the productFeedMapping.
The model that I need to dynamically modify is below:
//need to dynamically change these
@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class Products
{
//need to dynamically change these
@XmlElement(name = "product")
private List<Product> products = null;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
I will be instantiating it in this method:
//unmarshall xml method
private Map<String, List<String>> unmarshallXml(URL url, ProductFeedMapping productFeedMapping) {
//delcare the map
Map<String, List<String>> map = new HashMap<String, List<String>>();
//INSTANTIATE PRODUCT MODEL HERE
}