1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
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;
34
import nu.xom.Document;
35
import nu.xom.Element;
36
import nu.xom.Serializer;
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>.
50
* @author Elliotte Rusty Harold
54
public class FlatXMLBudget {
56
public static void convert(List data, OutputStream out)
59
Element budget = new Element("Budget");
60
Document doc = new Document(budget);
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
// 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);
78
budget.appendChild(lineItem);
81
Serializer serializer = new Serializer(out, "UTF-8");
82
serializer.write(doc);
87
public static void main(String[] args) {
91
if (args.length < 1) {
92
System.out.println("Usage: nu.xom.samples.FlatXMLBudget infile outfile");
96
InputStream in = new FileInputStream(args[0]);
98
if (args.length < 2) {
102
out = new FileOutputStream(args[1]);
105
List results = BudgetData.parse(in);
106
convert(results, out);
108
catch (IOException ex) {
109
System.err.println(ex);
b'\\ No newline at end of file'