KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > FlatXMLBudget


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import nu.xom.Document;
35 import nu.xom.Element;
36 import nu.xom.Serializer;
37
38
39 /**
40  *
41  * <p>
42  * Demonstrates building a structured XML document,
43  * from flat, tabular data. A different version of this
44  * example was originally developed for Chapter 4 of
45  * <cite><a target="_top"
46  * HREF="http://www.cafeconleche.org/books/xmljava/">Processing
47  * XML with Java</a></cite>.
48  * </p>
49  *
50  * @author Elliotte Rusty Harold
51  * @version 1.0
52  *
53  */

54 public class FlatXMLBudget {
55
56   public static void convert(List JavaDoc data, OutputStream JavaDoc out)
57    throws IOException JavaDoc {
58       
59     Element budget = new Element("Budget");
60     Document doc = new Document(budget);
61     
62     Iterator JavaDoc records = data.iterator();
63     while (records.hasNext()) {
64       Element lineItem = new Element("LineItem");
65       Map JavaDoc record = (Map JavaDoc) records.next();
66       Set JavaDoc fields = record.entrySet();
67       Iterator JavaDoc entries = fields.iterator();
68       while (entries.hasNext()) {
69         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
70         String JavaDoc name = (String JavaDoc) entry.getKey();
71         String JavaDoc value = (String JavaDoc) entry.getValue();
72         // some of the values contain ampersands and less than
73
// signs that must be escaped
74
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 JavaDoc[] 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 JavaDoc in = new FileInputStream JavaDoc(args[0]);
97       OutputStream JavaDoc out;
98       if (args.length < 2) {
99         out = System.out;
100       }
101       else {
102         out = new FileOutputStream JavaDoc(args[1]);
103       }
104
105       List JavaDoc results = BudgetData.parse(in);
106       convert(results, out);
107     }
108     catch (IOException JavaDoc ex) {
109       System.err.println(ex);
110     }
111   
112   }
113
114 }
Popular Tags