How to convert Map to xml based on Schema.

Discussion in 'Java' started by Mausam, Nov 17, 2012.

  1. Mausam

    Mausam Guest

    Hello,

    I have a Map (it can be nested map, containing of maps) and a schema. Keys in Map represent element/attribute name of the schema and an entry in Map will be at same depth as an element defined in schema

    I need help in generating xml from the map as per the schema provided, without generating new files (as e.g Jaxb would require) Sample schema/map provided below

    e.g Map = [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]] and schema will be

    e.g Schema
    <?xml version="1.0" encoding="windows-1252" ?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:element name="depts">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="dept" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="deptno" type="xsd:string"/><!-- This simple element e.g can be attribute in some schema-->
    <xsd:element name="dname" type="xsd:string"/>
    <xsd:element name="emps" maxOccurs="unbounded" minOccurs="0">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="empno" type="xsd:string"/>
    <xsd:element name="ename" type="xsd:string"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>

    -Many thanks
    Mausam, Nov 17, 2012
    #1
    1. Advertising

  2. Mausam

    Roedy Green Guest

    On Fri, 16 Nov 2012 22:52:46 -0800 (PST), Mausam
    <> wrote, quoted or indirectly quoted someone who
    said :

    >
    >I have a Map (it can be nested map,

    containing of maps) and a schema. Keys in Map represent
    element/attribute name of the schema and an entry in Map will be at
    same depth as an element defined in schema

    Presuming you can chase/span/visit nodes of your MAP tree and build a
    parse tree in RAM, element by element see
    http://mindprod.com/jgloss/xml.html#WRITING
    for how to get a linear stream of XML text out of it.

    You might want to read up on the visitor pattern so that each node can
    convert itself.

    see http://mindprod.com/jgloss/visitor.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Types of Garbage Collection:
    ()In Canada, the government sends men to your house every every week
    to take away your garbage. Hoarders are free to hang onto things
    they don't really need.
    ()In third world countries, it is up to you to take your own garbage away.
    ()Java's garbage collection system is analogous to a garbage removal
    system where every hour, workers scan your house for junk mail, the
    contents of waste baskets, carpet lint, toenail clippings and anything
    else they are absolutely sure you don't want to keep.
    ()C++'s system for disposing of unreferenced objects is similar to India's,
    with the strange feature that undiscarded garbage becomes invisible but
    still stinks.
    Roedy Green, Nov 17, 2012
    #2
    1. Advertising

  3. Mausam

    Mausam Guest

    Thanks Roedy,

    What I understand is build a model from XSD, and then loop the map for all the data and populate the model from data in Map.

    Was wondering if there is a better/efficient way to do it, or existing API to do that, so that I do not have to reinvent the wheel.

    -

    On Saturday, November 17, 2012 2:18:48 PM UTC+5:30, Roedy Green wrote:
    > On Fri, 16 Nov 2012 22:52:46 -0800 (PST), Mausam
    >
    > <> wrote, quoted or indirectly quoted someone who
    >
    > said :
    >
    >
    >
    > >

    >
    > >I have a Map (it can be nested map,

    >
    > containing of maps) and a schema. Keys in Map represent
    >
    > element/attribute name of the schema and an entry in Map will be at
    >
    > same depth as an element defined in schema
    >
    >
    >
    > Presuming you can chase/span/visit nodes of your MAP tree and build a
    >
    > parse tree in RAM, element by element see
    >
    > http://mindprod.com/jgloss/xml.html#WRITING
    >
    > for how to get a linear stream of XML text out of it.
    >
    >
    >
    > You might want to read up on the visitor pattern so that each node can
    >
    > convert itself.
    >
    >
    >
    > see http://mindprod.com/jgloss/visitor.html
    >
    > --
    >
    > Roedy Green Canadian Mind Products http://mindprod.com
    >
    > Types of Garbage Collection:
    >
    > ()In Canada, the government sends men to your house every every week
    >
    > to take away your garbage. Hoarders are free to hang onto things
    >
    > they don't really need.
    >
    > ()In third world countries, it is up to you to take your own garbage away.
    >
    > ()Java's garbage collection system is analogous to a garbage removal
    >
    > system where every hour, workers scan your house for junk mail, the
    >
    > contents of waste baskets, carpet lint, toenail clippings and anything
    >
    > else they are absolutely sure you don't want to keep.
    >
    > ()C++'s system for disposing of unreferenced objects is similar to India's,
    >
    > with the strange feature that undiscarded garbage becomes invisible but
    >
    > still stinks.
    Mausam, Nov 18, 2012
    #3
  4. Mausam

    Roedy Green Guest

    On Sat, 17 Nov 2012 20:14:10 -0800 (PST), Mausam
    <> wrote, quoted or indirectly quoted someone who
    said :

    >What I understand is build a model from XSD, and then loop the map for all the data and populate the model from data in Map.
    >
    >Was wondering if there is a better/efficient way to do it, or existing API to do that, so that I do not have to reinvent the wheel.
    >


    I don't how there could be. Your structure is something you made up.
    Why would any API or utility you did not write be able to eat it
    directly?

    If you have code to chase YOUR tree your ar 90% of the way there. You
    It just walks the tree, calling convertToXSD for each node.

    convertToXSD would implement an interface or abstract class, with code
    for most nodes identical.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Types of Garbage Collection:
    ()In Canada, the government sends men to your house every every week
    to take away your garbage. Hoarders are free to hang onto things
    they don't really need.
    ()In third world countries, it is up to you to take your own garbage away.
    ()Java's garbage collection system is analogous to a garbage removal
    system where every hour, workers scan your house for junk mail, the
    contents of waste baskets, carpet lint, toenail clippings and anything
    else they are absolutely sure you don't want to keep.
    ()C++'s system for disposing of unreferenced objects is similar to India's,
    with the strange feature that undiscarded garbage becomes invisible but
    still stinks.
    Roedy Green, Nov 18, 2012
    #4
  5. Mausam

    Arne Vajhøj Guest

    On 11/17/2012 1:52 AM, Mausam wrote:
    > I have a Map (it can be nested map, containing of maps) and a schema. Keys in Map represent element/attribute name of the schema and an entry in Map will be at same depth as an element defined in schema
    >
    > I need help in generating xml from the map as per the schema provided, without generating new files (as e.g Jaxb would require) Sample schema/map provided below
    >
    > e.g Map = [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]] and schema will be
    >
    > e.g Schema
    > <?xml version="1.0" encoding="windows-1252" ?>
    > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    > <xsd:element name="depts">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="dept" maxOccurs="unbounded">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="deptno" type="xsd:string"/><!-- This simple element e.g can be attribute in some schema-->
    > <xsd:element name="dname" type="xsd:string"/>
    > <xsd:element name="emps" maxOccurs="unbounded" minOccurs="0">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="empno" type="xsd:string"/>
    > <xsd:element name="ename" type="xsd:string"/>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:schema>


    I would probably:
    - generate a Java class from the schema (xjc)
    - populate from Map to an instance of that class via recursion
    and reflection
    - serialize instance to XML via JAXB

    If you have high performance requirements, then you may need
    to do some custom coding.

    Arne
    Arne Vajhøj, Nov 18, 2012
    #5
  6. Mausam

    Arne Vajhøj Guest

    On 11/18/2012 4:15 PM, Arne Vajhøj wrote:
    > On 11/17/2012 1:52 AM, Mausam wrote:
    >> I have a Map (it can be nested map, containing of maps) and a schema.
    >> Keys in Map represent element/attribute name of the schema and an
    >> entry in Map will be at same depth as an element defined in schema
    >>
    >> I need help in generating xml from the map as per the schema provided,
    >> without generating new files (as e.g Jaxb would require) Sample
    >> schema/map provided below
    >>
    >> e.g Map =
    >> [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]]
    >> and schema will be
    >>
    >> e.g Schema
    >> <?xml version="1.0" encoding="windows-1252" ?>
    >> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    >> elementFormDefault="qualified">
    >> <xsd:element name="depts">
    >> <xsd:complexType>
    >> <xsd:sequence>
    >> <xsd:element name="dept" maxOccurs="unbounded">
    >> <xsd:complexType>
    >> <xsd:sequence>
    >> <xsd:element name="deptno" type="xsd:string"/><!-- This
    >> simple element e.g can be attribute in some schema-->
    >> <xsd:element name="dname" type="xsd:string"/>
    >> <xsd:element name="emps" maxOccurs="unbounded"
    >> minOccurs="0">
    >> <xsd:complexType>
    >> <xsd:sequence>
    >> <xsd:element name="empno" type="xsd:string"/>
    >> <xsd:element name="ename" type="xsd:string"/>
    >> </xsd:sequence>
    >> </xsd:complexType>
    >> </xsd:element>
    >> </xsd:sequence>
    >> </xsd:complexType>
    >> </xsd:element>
    >> </xsd:sequence>
    >> </xsd:complexType>
    >> </xsd:element>
    >> </xsd:schema>

    >
    > I would probably:
    > - generate a Java class from the schema (xjc)
    > - populate from Map to an instance of that class via recursion
    > and reflection
    > - serialize instance to XML via JAXB
    >
    > If you have high performance requirements, then you may need
    > to do some custom coding.


    Demo with Map:

    import java.beans.IntrospectionException;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Map;

    public class AutoMapper {
    private static Object xctor(Object o, String propnam) throws
    IntrospectionException, InstantiationException, IllegalAccessException {
    PropertyDescriptor pd = new PropertyDescriptor(propnam, o.getClass());
    return pd.getPropertyType().newInstance();
    }
    private static void xset(Object o, String propnam, Object val) throws
    IntrospectionException, IllegalArgumentException,
    IllegalAccessException, InvocationTargetException {
    PropertyDescriptor pd = new PropertyDescriptor(propnam, o.getClass());
    pd.getWriteMethod().invoke(o, val);
    }
    @SuppressWarnings("unchecked")
    private static void map(Map<String,Object> m, Object o) throws
    IllegalArgumentException, IntrospectionException,
    IllegalAccessException, InvocationTargetException, InstantiationException {
    for(String key : m.keySet()) {
    Object val = m.get(key);
    if(val instanceof Map) {
    Object o2 = xctor(o, key);
    map((Map<String, Object>) val, o2);
    xset(o, key, o2);
    } else {
    xset(o, key, val);
    }
    }
    }
    public static void main(String[] args) throws Exception {
    Map<String,Object> m2 = new HashMap<String,Object>();
    m2.put("iv", 456);
    m2.put("xv", 123.456);
    Map<String,Object> m = new HashMap<String,Object>();
    m.put("iv", 123);
    m.put("sv", "ABC");
    m.put("cv", m2);
    Data o = new Data();
    System.out.println(m);
    map(m, o);
    System.out.println(o);
    }
    }

    class Data {
    private int iv;
    private String sv;
    private SubData cv;
    public int getIv() {
    return iv;
    }
    public void setIv(int iv) {
    this.iv = iv;
    }
    public String getSv() {
    return sv;
    }
    public void setSv(String sv) {
    this.sv = sv;
    }
    public SubData getCv() {
    return cv;
    }
    public void setCv(SubData cv) {
    this.cv = cv;
    }
    @Override
    public String toString() {
    return "(iv=" + iv + ",sv=" + sv + ",cv=" + cv + ")";
    }
    }

    class SubData {
    private int iv;
    private double xv;
    public int getIv() {
    return iv;
    }
    public void setIv(int iv) {
    this.iv = iv;
    }
    public double getXv() {
    return xv;
    }
    public void setXv(double xv) {
    this.xv = xv;
    }
    @Override
    public String toString() {
    return "(iv=" + iv + ",xv=" + xv + ")";
    }
    }

    Arne

    PS: It looks like you may really need to convert List not Map.
    Arne Vajhøj, Nov 19, 2012
    #6
  7. Mausam

    Mausam Guest

    Thanks a lot Arne for taking up the time to write code. However we do not want 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øj wrote:
    > On 11/18/2012 4:15 PM, Arne Vajh�j wrote:
    >
    > > On 11/17/2012 1:52 AM, Mausam wrote:

    >
    > >> I have a Map (it can be nested map, containing of maps) and a schema.

    >
    > >> Keys in Map represent element/attribute name of the schema and an

    >
    > >> entry in Map will be at same depth as an element defined in schema

    >
    > >>

    >
    > >> I need help in generating xml from the map as per the schema provided,

    >
    > >> without generating new files (as e.g Jaxb would require) Sample

    >
    > >> schema/map provided below

    >
    > >>

    >
    > >> e.g Map =

    >
    > >> [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]]

    >
    > >> and schema will be

    >
    > >>

    >
    > >> e.g Schema

    >
    > >> <?xml version="1.0" encoding="windows-1252" ?>

    >
    > >> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    >
    > >> elementFormDefault="qualified">

    >
    > >> <xsd:element name="depts">

    >
    > >> <xsd:complexType>

    >
    > >> <xsd:sequence>

    >
    > >> <xsd:element name="dept" maxOccurs="unbounded">

    >
    > >> <xsd:complexType>

    >
    > >> <xsd:sequence>

    >
    > >> <xsd:element name="deptno" type="xsd:string"/><!-- This

    >
    > >> simple element e.g can be attribute in some schema-->

    >
    > >> <xsd:element name="dname" type="xsd:string"/>

    >
    > >> <xsd:element name="emps" maxOccurs="unbounded"

    >
    > >> minOccurs="0">

    >
    > >> <xsd:complexType>

    >
    > >> <xsd:sequence>

    >
    > >> <xsd:element name="empno" type="xsd:string"/>

    >
    > >> <xsd:element name="ename" type="xsd:string"/>

    >
    > >> </xsd:sequence>

    >
    > >> </xsd:complexType>

    >
    > >> </xsd:element>

    >
    > >> </xsd:sequence>

    >
    > >> </xsd:complexType>

    >
    > >> </xsd:element>

    >
    > >> </xsd:sequence>

    >
    > >> </xsd:complexType>

    >
    > >> </xsd:element>

    >
    > >> </xsd:schema>

    >
    > >

    >
    > > I would probably:

    >
    > > - generate a Java class from the schema (xjc)

    >
    > > - populate from Map to an instance of that class via recursion

    >
    > > and reflection

    >
    > > - serialize instance to XML via JAXB

    >
    > >

    >
    > > If you have high performance requirements, then you may need

    >
    > > to do some custom coding.

    >
    >
    >
    > Demo with Map:
    >
    >
    >
    > import java.beans.IntrospectionException;
    >
    > import java.beans.PropertyDescriptor;
    >
    > import java.lang.reflect.InvocationTargetException;
    >
    > import java.util.HashMap;
    >
    > import java.util.Map;
    >
    >
    >
    > public class AutoMapper {
    >
    > private static Object xctor(Object o, String propnam) throws
    >
    > IntrospectionException, InstantiationException, IllegalAccessException {
    >
    > PropertyDescriptor pd = new PropertyDescriptor(propnam, o.getClass());
    >
    > return pd.getPropertyType().newInstance();
    >
    > }
    >
    > private static void xset(Object o, String propnam, Object val) throws
    >
    > IntrospectionException, IllegalArgumentException,
    >
    > IllegalAccessException, InvocationTargetException {
    >
    > PropertyDescriptor pd = new PropertyDescriptor(propnam, o.getClass());
    >
    > pd.getWriteMethod().invoke(o, val);
    >
    > }
    >
    > @SuppressWarnings("unchecked")
    >
    > private static void map(Map<String,Object> m, Object o) throws
    >
    > IllegalArgumentException, IntrospectionException,
    >
    > IllegalAccessException, InvocationTargetException, InstantiationException{
    >
    > for(String key : m.keySet()) {
    >
    > Object val = m.get(key);
    >
    > if(val instanceof Map) {
    >
    > Object o2 = xctor(o, key);
    >
    > map((Map<String, Object>) val, o2);
    >
    > xset(o, key, o2);
    >
    > } else {
    >
    > xset(o, key, val);
    >
    > }
    >
    > }
    >
    > }
    >
    > public static void main(String[] args) throws Exception {
    >
    > Map<String,Object> m2 = new HashMap<String,Object>();
    >
    > m2.put("iv", 456);
    >
    > m2.put("xv", 123.456);
    >
    > Map<String,Object> m = new HashMap<String,Object>();
    >
    > m.put("iv", 123);
    >
    > m.put("sv", "ABC");
    >
    > m.put("cv", m2);
    >
    > Data o = new Data();
    >
    > System.out.println(m);
    >
    > map(m, o);
    >
    > System.out.println(o);
    >
    > }
    >
    > }
    >
    >
    >
    > class Data {
    >
    > private int iv;
    >
    > private String sv;
    >
    > private SubData cv;
    >
    > public int getIv() {
    >
    > return iv;
    >
    > }
    >
    > public void setIv(int iv) {
    >
    > this.iv = iv;
    >
    > }
    >
    > public String getSv() {
    >
    > return sv;
    >
    > }
    >
    > public void setSv(String sv) {
    >
    > this.sv = sv;
    >
    > }
    >
    > public SubData getCv() {
    >
    > return cv;
    >
    > }
    >
    > public void setCv(SubData cv) {
    >
    > this.cv = cv;
    >
    > }
    >
    > @Override
    >
    > public String toString() {
    >
    > return "(iv=" + iv + ",sv=" + sv + ",cv=" + cv + ")";
    >
    > }
    >
    > }
    >
    >
    >
    > class SubData {
    >
    > private int iv;
    >
    > private double xv;
    >
    > public int getIv() {
    >
    > return iv;
    >
    > }
    >
    > public void setIv(int iv) {
    >
    > this.iv = iv;
    >
    > }
    >
    > public double getXv() {
    >
    > return xv;
    >
    > }
    >
    > public void setXv(double xv) {
    >
    > this.xv = xv;
    >
    > }
    >
    > @Override
    >
    > public String toString() {
    >
    > return "(iv=" + iv + ",xv=" + xv + ")";
    >
    > }
    >
    > }
    >
    >
    >
    > Arne
    >
    >
    >
    > PS: It looks like you may really need to convert List not Map.
    Mausam, Nov 19, 2012
    #7
  8. On 11/17/2012 02:52 AM, Mausam wrote:
    > Hello,
    >
    > I have a Map (it can be nested map, containing of maps) and a schema. Keys in Map represent element/attribute name of the schema and an entry in Map will be at same depth as an element defined in schema
    >
    > I need help in generating xml from the map as per the schema provided, without generating new files (as e.g Jaxb would require) Sample schema/map provided below
    >
    > e.g Map = [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]] and schema will be
    >
    > e.g Schema
    > <?xml version="1.0" encoding="windows-1252" ?>
    > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    > <xsd:element name="depts">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="dept" maxOccurs="unbounded">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="deptno" type="xsd:string"/><!-- This simple element e.g can be attribute in some schema-->
    > <xsd:element name="dname" type="xsd:string"/>
    > <xsd:element name="emps" maxOccurs="unbounded" minOccurs="0">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="empno" type="xsd:string"/>
    > <xsd:element name="ename" type="xsd:string"/>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:schema>
    >
    > -Many thanks
    >

    One question I have is, how and when is the generation supposed to occur?

    IOW, what is the environment?

    And where did the Map come from? If, as you replied to Arne, your schema
    can change, what process is responsible for creating new Maps that are
    relevant for updated schemas? Such a map, after all, should be a Java
    analog of an XML instance for the target schema, you don't need the
    schema at that point to generate a valid XML.

    AHS
    Arved Sandstrom, Nov 19, 2012
    #8
  9. Mausam

    Arne Vajhøj Guest

    On 11/19/2012 5:22 AM, Mausam wrote:
    > Thanks a lot Arne for taking up the time to write code. However we do
    > not want to generate java files everytime we get a different schema.
    > Schema and values are not fixed to one or two datatypes.


    There are nothing in the concept that limits it to
    one or two data types (actually the example used three!).

    And it is not clear why it is a bigger problem to add
    a class file compiled from Java generated from schema
    than to add the schema. In both cases you some code.

    Arne
    Arne Vajhøj, Nov 19, 2012
    #9
  10. Mausam

    Mausam Guest

    On Monday, November 19, 2012 4:29:15 PM UTC+5:30, Arved Sandstrom wrote:
    > On 11/17/2012 02:52 AM, Mausam wrote:
    >
    > > Hello,

    >
    > >

    >
    > > I have a Map (it can be nested map, containing of maps) and a schema. Keys in Map represent element/attribute name of the schema and an entry in Map will be at same depth as an element defined in schema

    >
    > >

    >
    > > I need help in generating xml from the map as per the schema provided, without generating new files (as e.g Jaxb would require) Sample schema/map provided below

    >
    > >

    >
    > > e.g Map = [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]] and schema will be

    >
    > >

    >
    > > e.g Schema

    >
    > > <?xml version="1.0" encoding="windows-1252" ?>

    >
    > > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    >
    > > <xsd:element name="depts">

    >
    > > <xsd:complexType>

    >
    > > <xsd:sequence>

    >
    > > <xsd:element name="dept" maxOccurs="unbounded">

    >
    > > <xsd:complexType>

    >
    > > <xsd:sequence>

    >
    > > <xsd:element name="deptno" type="xsd:string"/><!-- This simple element e.g can be attribute in some schema-->

    >
    > > <xsd:element name="dname" type="xsd:string"/>

    >
    > > <xsd:element name="emps" maxOccurs="unbounded" minOccurs="0">

    >
    > > <xsd:complexType>

    >
    > > <xsd:sequence>

    >
    > > <xsd:element name="empno" type="xsd:string"/>

    >
    > > <xsd:element name="ename" type="xsd:string"/>

    >
    > > </xsd:sequence>

    >
    > > </xsd:complexType>

    >
    > > </xsd:element>

    >
    > > </xsd:sequence>

    >
    > > </xsd:complexType>

    >
    > > </xsd:element>

    >
    > > </xsd:sequence>

    >
    > > </xsd:complexType>

    >
    > > </xsd:element>

    >
    > > </xsd:schema>

    >
    > >

    >
    > > -Many thanks

    >
    > >

    >
    > One question I have is, how and when is the generation supposed to occur?
    >
    >
    >
    > IOW, what is the environment?
    >
    >
    >
    > And where did the Map come from? If, as you replied to Arne, your schema
    >
    > can change, what process is responsible for creating new Maps that are
    >
    > relevant for updated schemas?


    There is an upstream process that does that.

    Such a map, after all, should be a Java
    >
    > analog of an XML instance for the target schema,


    You are correct. The map is Java analog of an XML Instance of the target schema. And I don't need a schema at this point but for two reasons:

    1) Map does not tell me if an single Entry is either an attribute in XML or Simple element.

    2) Map does not tell me the order of siblings (e.g sequence)

    you don't need the
    >
    > schema at that point to generate a valid XML.
    >
    >
    >
    > AHS
    Mausam, Nov 20, 2012
    #10
  11. Mausam

    Stuart Guest

    Am 11/17/12 7:52 AM, schrieb Mausam:
    > Hello,
    >
    > I have a Map (it can be nested map, containing of maps) and a schema. Keys in Map represent element/attribute name of the schema and an entry in Map will be at same depth as an element defined in schema
    >
    > I need help in generating xml from the map as per the schema provided, without generating new files (as e.g Jaxb would require) Sample schema/map provided below
    >
    > e.g Map = [dept=[deptno="10",dname="ABC",loc="XYZ",emps=[[empno=1000,ename="Albert"],[empno=2000,ename="John"]]]] and schema will be
    >
    > e.g Schema
    > <?xml version="1.0" encoding="windows-1252" ?>
    > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    > <xsd:element name="depts">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="dept" maxOccurs="unbounded">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="deptno" type="xsd:string"/><!-- This simple element e.g can be attribute in some schema-->
    > <xsd:element name="dname" type="xsd:string"/>
    > <xsd:element name="emps" maxOccurs="unbounded" minOccurs="0">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="empno" type="xsd:string"/>
    > <xsd:element name="ename" type="xsd:string"/>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:sequence>
    > </xsd:complexType>
    > </xsd:element>
    > </xsd:schema>
    >
    > -Many thanks
    >


    Try this: http://codeviewer.org/view/code:2c08 (sorry, but code posted
    on usenet looks really ugly). Note that this code can only handle a
    schema that looks like the one you have provided (no support for
    xsd:choice, schema must be in Russian Doll format, no proper quoting of
    special characters like <>" which are not allowed in XML string).

    Regards,
    Stuart
    Stuart, Nov 20, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,491
    Markus
    Nov 23, 2005
  2. Klaus Johannes Rusch

    Re: Convert DB2 schema to XML Schema

    Klaus Johannes Rusch, Aug 6, 2003, in forum: XML
    Replies:
    0
    Views:
    548
    Klaus Johannes Rusch
    Aug 6, 2003
  3. Andy
    Replies:
    0
    Views:
    523
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,163
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    2,783
Loading...

Share This Page