CHAPTER 9 ■ WRITING AND READING XML
public class Poem {
private static String title = "The Great Figure";
private static String author ="William Carlos Williams";
private static ArrayList<String> lines = new ArrayList<String>();
static {
lines.add("Among the rain");
lines.add("and lights");
lines.add("I saw the figure 5");
lines.add("in gold");
lines.add("on a red");
lines.add("fire truck");
lines.add("moving");
lines.add("tense");
lines.add("unheeded");
lines.add("to gong clangs");
lines.add("siren howls");
lines.add("and wheels rumbling");
lines.add("through the dark city");
}
public static String getTitle() {
return title;
}
public static String getAuthor() {
return author;
}
public static ArrayList<String> getLines() {
return lines;
}
}
As you can see, it's a pretty simple representation of a poem. Notice that it's also entirely static.
Some classes consist entirely of static members, but those classes usually define sets of helper methods
(string manipulation specialized for a particular application is a common use for that kind of helper
class). A class with static data, though, usually indicates that someone hasn't thought through a problem
very well. This kind of thing usually belongs in a file. For our purposes, though, this slightly odd class will
serve well enough.
Writing XML with DOM
Here's the code for writing an XML file with DOM, given the Poem class as the data source:
Listing 9-4. Writing XML with DOM
package com.bryantcs.examples.xml;
import java.io.File;