[xml]java.util.Map to xml

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[xml]java.util.Map to xml

Philip Donaghy
Hi,

Does anyone know the fastest way from a Map to xml?

I heard of an XML Serializer at Apache, but I can't
find it.

Philip


               
__________________________________
Discover Yahoo!
Stay in touch with email, IM, photo sharing and more. Check it out!
http://discover.yahoo.com/stayintouch.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [xml]java.util.Map to xml

Christian Aust
Hi,

Philip Donaghy schrieb:
> Does anyone know the fastest way from a Map to xml?

have a look a commons-betwixt:

BeanWriter writer = new BeanWriter(System.out);
Map m = new HashMap();
m.put("myKey", "some Value");
writer.write(m);

will write:

<?xml version="1.0" ?>
   <HashMap id="1">
     <empty>false</empty>
     <entry id="2">
       <key>myKey</key>
       <value>some Value</value>
     </entry>
   </HashMap>

The process is fully configurable. Fast enough? Regards,

-  Christian

--

Christian Aust
mailto:[hidden email]
icq: 84500990 - Yahoo!: datenimperator - MSN: datenimperator
PGP: A94E 0181 664D 27E3 F05A  A751 6A7E 90D1 A0A3 DEC7

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [xml]java.util.Map to xml

Philip Donaghy
Christian,

Thanks for the code, I remember seeing this when I
looked at commons-sql if I recall it uses
commons-digester. Thanks Craig and the authors of
commons-sql and commons-betwixt, James.

Philip

--- Christian Aust <[hidden email]> wrote:

> Hi,
>
> Philip Donaghy schrieb:
> > Does anyone know the fastest way from a Map to
> xml?
>
> have a look a commons-betwixt:
>
> BeanWriter writer = new BeanWriter(System.out);
> Map m = new HashMap();
> m.put("myKey", "some Value");
> writer.write(m);
>
> will write:
>
> <?xml version="1.0" ?>
>    <HashMap id="1">
>      <empty>false</empty>
>      <entry id="2">
>        <key>myKey</key>
>        <value>some Value</value>
>      </entry>
>    </HashMap>
>
> The process is fully configurable. Fast enough?
> Regards,
>
> -  Christian
>
> --
>
> Christian Aust
> mailto:[hidden email]
> icq: 84500990 - Yahoo!: datenimperator - MSN:
> datenimperator
> PGP: A94E 0181 664D 27E3 F05A  A751 6A7E 90D1 A0A3
> DEC7
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [hidden email]
> For additional commands, e-mail:
> [hidden email]
>
>



               
__________________________________
Discover Yahoo!
Stay in touch with email, IM, photo sharing and more. Check it out!
http://discover.yahoo.com/stayintouch.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [xml]java.util.Map to xml

Manos Batsis
In reply to this post by Philip Donaghy

Quoting Philip Donaghy <[hidden email]>:
> Does anyone know the fastest way from a Map to xml?

Lots of solutions here that can serialize java objects (and collections
like Maps) to XML; J2SE's XMLEncoder [1], XStream[2], JOX [3] and even
my own uriqa4j[4] can do that if you like RDF. There are many others of
course, some more binding oriented than others. XMLEncoder is already in
your classpath while XStream is a two-liner; what's best for you depends
on your needs though.

waitaminute. is this commons? i thought it was xml-dev :-)

[1] http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html
[2] http://xstream.codehaus.org/
[3] http://www.wutka.com/jox.html
[4] http://geekologue.com/uriqa4j/

hth,

Manos


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt]java.util.Map to xml

Philip Donaghy
In reply to this post by Philip Donaghy
Thanks for all the help, I forgot to mention the fact
that I have to go back to a the original Map with the
right objects. I see that my String, Long, Double
values are not encoded.

Can I get the Map back in it's original state?

Philip

--- Philip Donaghy <[hidden email]> wrote:

> Christian,
>
> Thanks for the code, I remember seeing this when I
> looked at commons-sql if I recall it uses
> commons-digester. Thanks Craig and the authors of
> commons-sql and commons-betwixt, James.
>
> Philip
>
> --- Christian Aust <[hidden email]> wrote:
>
> > Hi,
> >
> > Philip Donaghy schrieb:
> > > Does anyone know the fastest way from a Map to
> > xml?
> >
> > have a look a commons-betwixt:
> >
> > BeanWriter writer = new BeanWriter(System.out);
> > Map m = new HashMap();
> > m.put("myKey", "some Value");
> > writer.write(m);
> >
> > will write:
> >
> > <?xml version="1.0" ?>
> >    <HashMap id="1">
> >      <empty>false</empty>
> >      <entry id="2">
> >        <key>myKey</key>
> >        <value>some Value</value>
> >      </entry>
> >    </HashMap>
> >
> > The process is fully configurable. Fast enough?
> > Regards,
> >
> > -  Christian
> >
> > --
> >
> > Christian Aust
> > mailto:[hidden email]
> > icq: 84500990 - Yahoo!: datenimperator - MSN:
> > datenimperator
> > PGP: A94E 0181 664D 27E3 F05A  A751 6A7E 90D1 A0A3
> > DEC7
> >
> >
>
---------------------------------------------------------------------

> > To unsubscribe, e-mail:
> > [hidden email]
> > For additional commands, e-mail:
> > [hidden email]
> >
> >
>
>
>
>
> __________________________________
> Discover Yahoo!
> Stay in touch with email, IM, photo sharing and
> more. Check it out!
> http://discover.yahoo.com/stayintouch.html
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [hidden email]
> For additional commands, e-mail:
> [hidden email]
>
>



               
__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/weekend.html 


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [xml]java.util.Map to xml

Philip Donaghy
In reply to this post by Manos Batsis
Manos,

Thanks for your kind response, the requirement I have
was to store in a Map instances implementing Map,
List, and all the primitive wrappers.

I tried XStream and got the following results,

9 juin 2005 17:09:20
net.agentis.multiagent.util.xml.MapEncoder main
INFO: State : {z=9.0, mylist=[5.5, 5, 5.5.5, Thu Jun
09 17:09:20 CEST 2005], y=9, state1={z=19.0, y=98,
x=asdf}, x=9}
9 juin 2005 17:09:20
net.agentis.multiagent.util.xml.MapEncoder main
INFO: Encoded State : <map>
  <entry>
    <string>z</string>
    <double>9.0</double>
  </entry>
  <entry>
    <string>mylist</string>
    <list>
      <double>5.5</double>
      <long>5</long>
      <string>5.5.5</string>
      <date>2005-06-09 17:09:20.693 CEST</date>
    </list>
  </entry>
  <entry>
    <string>y</string>
    <long>9</long>
  </entry>
  <entry>
    <string>state1</string>
    <map>
      <entry>
        <string>z</string>
        <double>19.0</double>
      </entry>
      <entry>
        <string>y</string>
        <long>98</long>
      </entry>
      <entry>
        <string>x</string>
        <string>asdf</string>
      </entry>
    </map>
  </entry>
  <entry>
    <string>x</string>
    <string>9</string>
  </entry>
</map>
9 juin 2005 17:09:20
net.agentis.multiagent.util.xml.MapEncoder main
INFO: Decoded State : {z=9.0, mylist=[5.5, 5, 5.5.5,
Thu Jun 09 17:09:20 CEST 2005], y=9, state1={z=19.0,
y=98, x=asdf}, x=9}
9 juin 2005 17:09:20
net.agentis.multiagent.util.xml.MapEncoder main
INFO: OriginalState : {z=9.0, mylist=[5.5, 5, 5.5.5,
Thu Jun 09 17:09:20 CEST 2005], y=9, state1={z=19.0,
y=98, x=asdf}, x=9}

It even supports Dates.

The code was really simple.

XStream xstream = new XStream(new DomDriver());

Map state = new HashMap();
state.put("x", "9");
state.put("y", new Long(9));
state.put("z", new Double(9.0));
Map state1 = new HashMap();
state1.put("x", "asdf");
state1.put("y", new Long(98));
state1.put("z", new Double(19.0));
state.put("state1", state1);
List list = new ArrayList();
list.add(new Double(5.5));
list.add(new Long(5));
list.add("5.5.5");
list.add(new java.util.Date());
state.put("mylist", list);
log.info("State : " + state.toString());
String encodedState = xstream.toXML(state);
log.info("Encoded State : " + encodedState);
Map newState = xstream.fromXML(encodedState);
log.info("Decoded State : " + newState.toString());
log.info("OriginalState : " + state.toString());

Thanks Manos,

Philip

--- Manos Batsis <[hidden email]> wrote:

>
> Quoting Philip Donaghy <[hidden email]>:
> > Does anyone know the fastest way from a Map to
> xml?
>
> Lots of solutions here that can serialize java
> objects (and collections
> like Maps) to XML; J2SE's XMLEncoder [1],
> XStream[2], JOX [3] and even
> my own uriqa4j[4] can do that if you like RDF. There
> are many others of
> course, some more binding oriented than others.
> XMLEncoder is already in
> your classpath while XStream is a two-liner; what's
> best for you depends
> on your needs though.
>
> waitaminute. is this commons? i thought it was
> xml-dev :-)
>
> [1]
>
http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [hidden email]
> For additional commands, e-mail:
> [hidden email]
>
>



               
__________________________________
Discover Yahoo!
Have fun online with music videos, cool games, IM and more. Check it out!
http://discover.yahoo.com/online.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]