1

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

}
Community
  • 1
  • 1
Phil Hudson
  • 3,819
  • 8
  • 35
  • 59
  • This is possible (see this: http://stackoverflow.com/questions/15239929/runtimeannoatationreader-to-read-custom-annotations), but your intention does not seem right. Why do you need to change annotation to unmarshal dynamically? Would you please elaborate a bit mor on your approach and intention? Seems interesting. – lexicore Oct 20 '14 at 15:16
  • Hi @lexicore many thanks for this. I've updated my question above. I'll look into what you linked me now! Thanks again – Phil Hudson Oct 20 '14 at 15:21
  • I'm still curious. :) Could you give an example of the user pecified mapping? Is it just about selection one of the models or is is completely "free"? JAXB is good for static or more-or-less static models. Having something completely dynamic may need another tool. If you elaborate on your case a bit you may get a better advice. Maybe you don't need JAXB or maybe you don't need dynamic annotation reader. – lexicore Oct 20 '14 at 15:28
  • @lexicore thanks again, I've updated again and tried to explain in more depth, please let me know if that helps :) – Phil Hudson Oct 20 '14 at 15:34
  • 1
    Ok, not that I understood everything but my gut feeling says you'll be better of without JAXB as your mapping is dynamic and you produce a very generic `Map>` model at the end. JAXB is good for static mappings. Either XML Schema (and kinds)-first or Java-first. Both "firsts" are quite static. I'm saying this and I'm a JAXB fan so take this into an account. :) – lexicore Oct 20 '14 at 15:49
  • Ah thanks very much for this, is there another dynamic mapper you could possibly recommend? Cheers – Phil Hudson Oct 20 '14 at 15:50
  • Oh, wait, I may be wrong. Check dynamic JAXB: http://docs.oracle.com/middleware/1212/toplink/TLJAX/dynamic_jaxb.htm But I don't have experience with this, sorry. – lexicore Oct 20 '14 at 15:55
  • @lexicore oh this looks interesting, I'm going to read up! – Phil Hudson Oct 20 '14 at 15:58

0 Answers0