| Serialize a Java Collection with the streaming API |
|
|
|
| Written by Charles | |||||
| Sunday, 08 November 2009 19:01 | |||||
|
Writing a Collection to xml is more efficiently done using xml streaming techniques rather than creating a DOM and then serializing it; unless, of course, you want to make use of the DOM other than purely as an object to serialize. import javax.xml.stream.*;
import java.io.*; import java.util.*; public class Map2Xml { public static void main(String[] args) { Map<String, Integer> m = Map2Xml.createRandomMap(); System.out.println("Starting map:"); System.out.println(m); // Now write it as xml try { Map2Xml.toXml(m, new FileWriter(args[0])); } catch(IOException e) { e.printStackTrace(); } catch(XMLStreamException e) { e.printStackTrace(); } } public static Map<String, Integer> createRandomMap() { Map<String, Integer> m = new HashMap<String, Integer>(5); for(char i = 'a';i < 'f';i++) { m.put(String.valueOf(i), (int)(Math.random() * 99)); } return m; } public static <K,V> void toXml(Map<K, V> map, Writer out) throws IOException, XMLStreamException { XMLStreamWriter xsw = null; try { try { XMLOutputFactory xof = XMLOutputFactory.newInstance(); // If you want pretty-printing, you can use: //xsw = new javanet.staxutils.IndentingXMLStreamWriter(xof.createXMLStreamWriter(out)); xsw = xof.createXMLStreamWriter(out); xsw.writeStartDocument("utf-8", "1.0"); xsw.writeStartElement("entries"); // Do the Collection for(Map.Entry<K, V> e : map.entrySet()) { xsw.writeStartElement("entry"); xsw.writeAttribute("key", e.getKey().toString()); xsw.writeAttribute("value", e.getValue().toString()); xsw.writeEndElement(); } xsw.writeEndElement(); xsw.writeEndDocument(); } finally { if (out != null) { try { out.close() ; } catch(IOException e) { /* ignore */ } } }// end inner finally } finally { if (xsw != null) { try { xsw.close() ; } catch(XMLStreamException e) { /* ignore */ } } } } }
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Sunday, 08 November 2009 23:03 ) |



