Transform java objects to dynamic xml -
i'm working dynamic xml table format consisting of schema specifies column names , types, , values tag contains rows. simplified version of xsd below:
<xs:complextype name="data"> <xs:sequence> <xs:element name="schema" type="schema"/> <xs:element name="values" type="values"/> </xs:sequence> </xs:complextype> <xs:complextype name="schema"> <xs:anyattribute/> </xs:complextype> <xs:complextype name="values"> <xs:anyattribute/> </xs:complextype> and example xml generated it:
<data> <schema firstname="string" lastname="string" age="integer"> <values> <value firstname="a" lastname="b" age="23"/> <value firstname="c" lastname="d" age="63"/> … </values> </data> data generate xml comes list of data objects, our example:
public class person { private string firstname; private string lastname; private int age; // getters , setters… } currently i'm creating adding code class (i'm using jaxb generate xml):
private static qname first_name = new qname("firstname"); private static qname last_name = new qname("lastname"); private static qname age = new qname("age"); private static map<qname, string> schema; static { schema = new hashmap<qname, string>(); schema.put(first_name, "string"); schema.put(last_name, "string"); schema.put(age, "integer"): } public map<qname, string> asmap() { map<qname, string> map = new hashmap<qname, string>(); map.put(first_name, firstname); map.put(last_name, lastname); map.put(age, integer.tostring(age)): return map; } public static map<qname, string> getschema() { return schema; } and each data object used generate xml. works though don't feel it's best solution. major problem see there no connection between schema , values, if changes in 1 of them have remember update other 1 too. it's bit noisy adding classes.
could suggest better (cleaner/more generic) way of doing this? maybe way auto generate schema and/or values maps? or other suggestion....
(it nicest if have worked given java bean without changing class, i'm fine decorating / adding things class if necessary.)
thanks
the whole problem table dynamic. need be?
if not, solution straightforward, because can make static table , bind java beans using jaxb.
if need dynamic, thing can link static instances of table java beans using custom binder. meaning if have static instance of table contains firstname, lastname , age, can write custom binder generate java beans table, have proper bindings (firstname, lastname , age), you're doing manually. problem approach writing binding tool.
but using off-the-shelf software, easiest thing not use dynamic table , instead write schemas each instance of dynamic table. xml documents these schemas still conform 1 defines dynamic table, allow use jaxb automate code generation , keep java classes in sync xml documents.
Comments
Post a Comment