0

I have some objects I want to marshal using JAXB. Using @XmlRootElement and @XmlAccessorType(XmlAccessType.FIELD) it’s easy. However, I would much prefer not to change the class in question since the code in question is outside of my responsibility and I need to marshal it only for debugging purposes.

Using the article JAXB - No Annotations Required I got rid of the @XmlRootElement, but how can I configure the field access programmatically? Is there any standard way?

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
  • I am not sure if this will work, but i think you can add the annotation `@XmlAccessorType(XmlAccessType.FIELD)` at runtime using Java reflection. http://stackoverflow.com/a/30287201/6371459. Make a recursive method for the object to marshall and apply also to inner objects (exclude java basic objects in `java.lang`) – pedrofb Dec 08 '16 at 17:14
  • This may work, yes. But it’s incredibly hacky. I don’t have the energy to try this out. I managed so sneak in a package-level annotation which was good enough for my special case (all classes are in the same package, and all require the same XML accessor type). – Michael Piefel Dec 12 '16 at 16:14
  • Jajajaja, sure It would be a risky solution – pedrofb Dec 12 '16 at 18:47
  • @Mickael , have you got solution for this – Vijay Jun 30 '19 at 15:06
  • No, @VijayGupta, at the time, I found the work-around, and I didn’t have that specific problem since. – Michael Piefel Jul 09 '19 at 18:41

0 Answers0