commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Philip Donaghy <pmdcomm...@yahoo.com>
Subject Re: [xml]java.util.Map to xml
Date Thu, 09 Jun 2005 15:20:24 GMT
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 <manos_lists@geekologue.com> wrote:

> 
> Quoting Philip Donaghy <pmdcommons@yahoo.com>: 
> > 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:
> commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-user-help@jakarta.apache.org
> 
> 



		
__________________________________ 
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message