Java Reference
In-Depth Information
LISTING 19.6
The Full Text of DomainWriter.java
1: import java.io.*;
2: import nu.xom.*;
3:
4: public class DomainWriter {
5: public static void main(String[] arguments) throws IOException {
6: try {
7: // Create a tree from an XML document
8: // specified as a command-line argument
9: Builder builder = new Builder();
10: Document doc = builder.build(arguments[0]);
11:
12: // Create a comment with the current time and date
13: Comment timestamp = new Comment(“File created “
14: + new java.util.Date());
15:
16: // Add the comment above everything else in the
17: // document
18: doc.insertChild(timestamp, 0);
19:
20: // Create a file output stream to a new file
21: File inFile = new File(arguments[0]);
22: FileOutputStream fos = new FileOutputStream(“new_” +
inFile.getName());
23:
24: // Using a serializer with indention set to 2 spaces,
25: // write the XML document to the file
26: Serializer output = new Serializer(fos, “ISO-8859-1”);
27: output.setIndent(2);
28: output.write(doc);
29: } catch (ParsingException pe) {
30: System.out.println(“Error parsing document: “ + pe.getMessage());
31: pe.printStackTrace();
32: System.exit(-1);
33: }
34: }
35: }
19
The DomainWriter application takes an XML filename as a command-line argument
when run:
java DomainWriter feeds2.rss
This command produces a file called new_feeds2.rss that contains an indented copy of
the XML document with a time stamp inserted as a comment.
 
Search WWH ::




Custom Search