Received: by 10.224.220.136 with SMTP id hy8mr8315646qab.3.1353320566776; Mon, 19 Nov 2012 02:22:46 -0800 (PST) Received: by 10.49.127.113 with SMTP id nf17mr2388824qeb.29.1353320566759; Mon, 19 Nov 2012 02:22:46 -0800 (PST) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!u2no12430621qal.0!news-out.google.com!gf5ni327qab.0!nntp.google.com!u2no12430619qal.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Mon, 19 Nov 2012 02:22:46 -0800 (PST) In-Reply-To: <50a99c86$0$281$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=148.87.67.211; posting-account=BE4JzgoAAACLX-aQYawit5MkTNnptKNs NNTP-Posting-Host: 148.87.67.211 References: <7e1a4b59-0c97-40ca-af6e-22277f62b469@googlegroups.com> <50a94fea$0$292$14726298@news.sunsite.dk> <50a99c86$0$281$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7d2a2881-e3fe-4d24-9f59-47b23041422e@googlegroups.com> Subject: Re: How to convert Map to xml based on Schema. From: Mausam Injection-Date: Mon, 19 Nov 2012 10:22:46 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 7530 Xref: csiph.com comp.lang.java.programmer:19809 Thanks a lot Arne for taking up the time to write code. However we do not w= ant to generate java files everytime we get a different schema. Schema and = values are not fixed to one or two datatypes. On Monday, November 19, 2012 8:12:15 AM UTC+5:30, Arne Vajh=F8j wrote: > On 11/18/2012 4:15 PM, Arne Vajh=EF=BF=BDj wrote: >=20 > > On 11/17/2012 1:52 AM, Mausam wrote: >=20 > >> I have a Map (it can be nested map, containing of maps) and a schema. >=20 > >> Keys in Map represent element/attribute name of the schema and an >=20 > >> entry in Map will be at same depth as an element defined in schema >=20 > >> >=20 > >> I need help in generating xml from the map as per the schema provided, >=20 > >> without generating new files (as e.g Jaxb would require) Sample >=20 > >> schema/map provided below >=20 > >> >=20 > >> e.g Map =3D >=20 > >> [dept=3D[deptno=3D"10",dname=3D"ABC",loc=3D"XYZ",emps=3D[[empno=3D1000= ,ename=3D"Albert"],[empno=3D2000,ename=3D"John"]]]] >=20 > >> and schema will be >=20 > >> >=20 > >> e.g Schema >=20 > >> >=20 > >> =20 > >> elementFormDefault=3D"qualified"> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> =20 > >> minOccurs=3D"0"> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > > >=20 > > I would probably: >=20 > > - generate a Java class from the schema (xjc) >=20 > > - populate from Map to an instance of that class via recursion >=20 > > and reflection >=20 > > - serialize instance to XML via JAXB >=20 > > >=20 > > If you have high performance requirements, then you may need >=20 > > to do some custom coding. >=20 >=20 >=20 > Demo with Map: >=20 >=20 >=20 > import java.beans.IntrospectionException; >=20 > import java.beans.PropertyDescriptor; >=20 > import java.lang.reflect.InvocationTargetException; >=20 > import java.util.HashMap; >=20 > import java.util.Map; >=20 >=20 >=20 > public class AutoMapper { >=20 > private static Object xctor(Object o, String propnam) throws=20 >=20 > IntrospectionException, InstantiationException, IllegalAccessException { >=20 > PropertyDescriptor pd =3D new PropertyDescriptor(propnam, o.getClass())= ; >=20 > return pd.getPropertyType().newInstance(); >=20 > } >=20 > private static void xset(Object o, String propnam, Object val) throws=20 >=20 > IntrospectionException, IllegalArgumentException,=20 >=20 > IllegalAccessException, InvocationTargetException { >=20 > PropertyDescriptor pd =3D new PropertyDescriptor(propnam, o.getClass())= ; >=20 > pd.getWriteMethod().invoke(o, val); >=20 > } >=20 > @SuppressWarnings("unchecked") >=20 > private static void map(Map m, Object o) throws=20 >=20 > IllegalArgumentException, IntrospectionException,=20 >=20 > IllegalAccessException, InvocationTargetException, InstantiationException= { >=20 > for(String key : m.keySet()) { >=20 > Object val =3D m.get(key); >=20 > if(val instanceof Map) { >=20 > Object o2 =3D xctor(o, key); >=20 > map((Map) val, o2); >=20 > xset(o, key, o2); >=20 > } else { >=20 > xset(o, key, val); >=20 > } >=20 > } >=20 > } >=20 > public static void main(String[] args) throws Exception { >=20 > Map m2 =3D new HashMap(); >=20 > m2.put("iv", 456); >=20 > m2.put("xv", 123.456); >=20 > Map m =3D new HashMap(); >=20 > m.put("iv", 123); >=20 > m.put("sv", "ABC"); >=20 > m.put("cv", m2); >=20 > Data o =3D new Data(); >=20 > System.out.println(m); >=20 > map(m, o); >=20 > System.out.println(o); >=20 > } >=20 > } >=20 >=20 >=20 > class Data { >=20 > private int iv; >=20 > private String sv; >=20 > private SubData cv; >=20 > public int getIv() { >=20 > return iv; >=20 > } >=20 > public void setIv(int iv) { >=20 > this.iv =3D iv; >=20 > } >=20 > public String getSv() { >=20 > return sv; >=20 > } >=20 > public void setSv(String sv) { >=20 > this.sv =3D sv; >=20 > } >=20 > public SubData getCv() { >=20 > return cv; >=20 > } >=20 > public void setCv(SubData cv) { >=20 > this.cv =3D cv; >=20 > } >=20 > @Override >=20 > public String toString() { >=20 > return "(iv=3D" + iv + ",sv=3D" + sv + ",cv=3D" + cv + ")"; >=20 > } >=20 > } >=20 >=20 >=20 > class SubData { >=20 > private int iv; >=20 > private double xv; >=20 > public int getIv() { >=20 > return iv; >=20 > } >=20 > public void setIv(int iv) { >=20 > this.iv =3D iv; >=20 > } >=20 > public double getXv() { >=20 > return xv; >=20 > } >=20 > public void setXv(double xv) { >=20 > this.xv =3D xv; >=20 > } >=20 > @Override >=20 > public String toString() { >=20 > return "(iv=3D" + iv + ",xv=3D" + xv + ")"; >=20 > } >=20 > } >=20 >=20 >=20 > Arne >=20 >=20 >=20 > PS: It looks like you may really need to convert List not Map.