You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jaime Garza <Ja...@newScale.com> on 2005/04/29 23:49:54 UTC
[Betwixt]: HashMap to XML and back
Can somebody help me learn how to read a HashMap back with
betwixt/digester?
When I use betwixt to write a HashMap I get a nice XML, but when I try
to read it back, I only get a null. I am using betwixt 0.6 and digester
1.5. Yes, this is a map of maps. This is the code
package com.newscale.test;
import java.util.Map;
import java.io.StringWriter;
import org.apache.commons.betwixt.io.BeanWriter;
import java.beans.IntrospectionException;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.*;
import java.io.StringReader;
import org.apache.commons.betwixt.io.BeanReader;
public class Betwixt {
private String convertToString(HashMap map) throws
IntrospectionException, SAXException, IOException {
if (map == null) {
return null;
}
StringWriter outputWriter = new StringWriter();
BeanWriter beanWriter = new BeanWriter(outputWriter);
beanWriter.enablePrettyPrint();
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimi
tives(false);
beanWriter.write("map", map);
return outputWriter.toString();
}
private HashMap convertToMap(String s) throws SAXException,
IOException, IntrospectionException {
StringReader reader = new StringReader(s);
BeanReader beanReader = new BeanReader();
beanReader.getBindingConfiguration().setMapIDs(false);
beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimi
tives(false);
beanReader.registerBeanClass("map", java.util.HashMap.class);
HashMap map = (HashMap) beanReader.parse(reader); // returns null
always!
return map;
}
public static void main(String[] args) throws Exception {
HashMap hash = new HashMap();
Map sub1 = new HashMap();
sub1.put("sub1i", new Integer(3));
sub1.put("sub1d", new Double(5.0));
sub1.put("sub1s", "this is a test & check");
hash.put("sub1", sub1);
Betwixt betwixtTester = new Betwixt();
String xml1 = null;
String xml2 = null;
xml1 = betwixtTester.convertToString(hash);
HashMap hash2 = betwixtTester.convertToMap(xml1);
xml2 = betwixtTester.convertToString(hash2);
System.out.println(xml1);
System.out.println("====================================================
==================");
System.out.println(xml2);
}
}
Output:
<map>
<entry>
<key>sub1</key>
<value>
<entry>
<key>sub1i</key>
<value>3</value>
</entry>
<entry>
<key>sub1s</key>
<value>this is a test & check</value>
</entry>
<entry>
<key>sub1d</key>
<value>5.0</value>
</entry>
</value>
</entry>
</map>
======================================================================
null
Re: [Betwixt]: HashMap to XML and back
Posted by Vikram Goyal <te...@craftbits.com>.
Hi,
I haven't actually run your code and not sure why you are getting null, but
you must realize that Betwixt relies on a addXXX method in the object to be
recreated for the hashmap to work. Since your hashmap of hashmap wouldn't
contain such a method, you need to extend the HashMap class and add such a
method in.
HTH,
Vikram Goyal
---------------------------------------------------------------------------------------
Jakarta Commons Online Bookshelf
Module 5: JXPath and Betwixt:
http://www.manning.com/catalog/view.php?book=goyal5
All Modules: http://www.manning.com/goyal
---------------------------------------------------------------------------------------
----- Original Message -----
From: "Jaime Garza" <Ja...@newScale.com>
To: <co...@jakarta.apache.org>
Sent: Saturday, April 30, 2005 7:49 AM
Subject: [Betwixt]: HashMap to XML and back
Can somebody help me learn how to read a HashMap back with
betwixt/digester?
When I use betwixt to write a HashMap I get a nice XML, but when I try
to read it back, I only get a null. I am using betwixt 0.6 and digester
1.5. Yes, this is a map of maps. This is the code
package com.newscale.test;
import java.util.Map;
import java.io.StringWriter;
import org.apache.commons.betwixt.io.BeanWriter;
import java.beans.IntrospectionException;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.*;
import java.io.StringReader;
import org.apache.commons.betwixt.io.BeanReader;
public class Betwixt {
private String convertToString(HashMap map) throws
IntrospectionException, SAXException, IOException {
if (map == null) {
return null;
}
StringWriter outputWriter = new StringWriter();
BeanWriter beanWriter = new BeanWriter(outputWriter);
beanWriter.enablePrettyPrint();
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimi
tives(false);
beanWriter.write("map", map);
return outputWriter.toString();
}
private HashMap convertToMap(String s) throws SAXException,
IOException, IntrospectionException {
StringReader reader = new StringReader(s);
BeanReader beanReader = new BeanReader();
beanReader.getBindingConfiguration().setMapIDs(false);
beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimi
tives(false);
beanReader.registerBeanClass("map", java.util.HashMap.class);
HashMap map = (HashMap) beanReader.parse(reader); // returns null
always!
return map;
}
public static void main(String[] args) throws Exception {
HashMap hash = new HashMap();
Map sub1 = new HashMap();
sub1.put("sub1i", new Integer(3));
sub1.put("sub1d", new Double(5.0));
sub1.put("sub1s", "this is a test & check");
hash.put("sub1", sub1);
Betwixt betwixtTester = new Betwixt();
String xml1 = null;
String xml2 = null;
xml1 = betwixtTester.convertToString(hash);
HashMap hash2 = betwixtTester.convertToMap(xml1);
xml2 = betwixtTester.convertToString(hash2);
System.out.println(xml1);
System.out.println("====================================================
==================");
System.out.println(xml2);
}
}
Output:
<map>
<entry>
<key>sub1</key>
<value>
<entry>
<key>sub1i</key>
<value>3</value>
</entry>
<entry>
<key>sub1s</key>
<value>this is a test & check</value>
</entry>
<entry>
<key>sub1d</key>
<value>5.0</value>
</entry>
</value>
</entry>
</map>
======================================================================
null
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org