1 21 22 package nu.xom.samples; 23 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import nu.xom.Document; 35 import nu.xom.Element; 36 import nu.xom.Serializer; 37 38 39 54 public class FlatXMLBudget { 55 56 public static void convert(List data, OutputStream out) 57 throws IOException { 58 59 Element budget = new Element("Budget"); 60 Document doc = new Document(budget); 61 62 Iterator records = data.iterator(); 63 while (records.hasNext()) { 64 Element lineItem = new Element("LineItem"); 65 Map record = (Map ) records.next(); 66 Set fields = record.entrySet(); 67 Iterator entries = fields.iterator(); 68 while (entries.hasNext()) { 69 Map.Entry entry = (Map.Entry ) entries.next(); 70 String name = (String ) entry.getKey(); 71 String value = (String ) entry.getValue(); 72 Element field = new Element(name); 75 field.appendChild(value); 76 lineItem.appendChild(field); 77 } 78 budget.appendChild(lineItem); 79 } 80 81 Serializer serializer = new Serializer(out, "UTF-8"); 82 serializer.write(doc); 83 serializer.flush(); 84 85 } 86 87 public static void main(String [] args) { 88 89 try { 90 91 if (args.length < 1) { 92 System.out.println("Usage: nu.xom.samples.FlatXMLBudget infile outfile"); 93 return; 94 } 95 96 InputStream in = new FileInputStream (args[0]); 97 OutputStream out; 98 if (args.length < 2) { 99 out = System.out; 100 } 101 else { 102 out = new FileOutputStream (args[1]); 103 } 104 105 List results = BudgetData.parse(in); 106 convert(results, out); 107 } 108 catch (IOException ex) { 109 System.err.println(ex); 110 } 111 112 } 113 114 } | Popular Tags |