How to serialize an object to XML with dynamic element's attribute?

What would be a dynamic and flexible way to transform XML or Json to Java object?

  • I am wondering that is there currently an API or a way to transform a XML or a JSON document into a Java object in a more dynamic and flexible way ? for example lets say this XML doesn't have the same element every time, sometimes it has 3 elements or sometimes it has 10 under the root. The naive way to implement it would be just manually hard-code all the possibilities in your java obj like in this case initialize all 10 instance variables, but I am looking for a more dynamic way that your obj can vary accordingly from xml document. In other words, if my XML document has 3 elements under the root then my java object would just have 3 instance variables.

  • Answer:

    I am not quite sure whether this could resolve your issue, but I recommend you to use protocol buffer. It is more dynamic, robust and you can create a corresponding Java class, if you use its API - http://code.google.com/apis/protocolbuffers/docs/overview.html

Yigit Ihlamur at Quora Visit the source

Was this solution helpful to you?

Other answers

Use the Jackson library in the mode where the JSON tree is represented as the Jackson node classes (JsonNode, ObjectNode, ArrayNode), instead of bound to a set of your own, custom domain classes. Wonderfully dynamic.

Cornel Masson

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.