XMLWriter¶
XMLWriter is a very simple Writer for XML files. It takes care for proper formatting, but does not support advanced XML features like namespaces.
The following example writes an XML file containing an Albert Einstein quote.
/*
* Examples for the jshred library
*
* Copyright (c) 2004 Richard "Shred" Körber
* http://www.shredzone.org/go/jshred
*
* This example is free software. Use and modify it at will.
*/
import net.shredzone.jshred.io.*;
import java.io.*;
/**
* This is an example how to use the XMLWriter to write XML files.
* It will create a file "test.xml" in the current directory, containing
* an Albert Einstein quote.
*/
public class XMLDemo {
/**
* Start this demo.
*
* @param args Shell arguments
*/
public static void main( String[] args ) {
XMLWriter xml = null;
try {
// Open the XMLWriter
xml = new XMLWriter(new FileWriter("test.xml"));
// Start the document
// The very first thing to do is to start the document. It will
// write the XML header containing the charset that is being
// used. This is obligatory!
xml.startDocument();
// Place a comment
// You can place comments anywhere and in any number.
xml.writeComment("This is a jshred generated XML file");
// Create an element
// This will open a new element. All subsequent data is written
// into that element until it is closed.
xml.startElement("quote");
// Write content
// You can fill the content of the current element. If you want
// to write a content with multiple lines, just write them line
// by line. XMLWriter takes care about proper escaping. Here the
// double quotes will be automatically escaped. You don't need
// to worry about it.
xml.writeContent("\"Education is what remains");
xml.writeContent("after one has forgotten");
xml.writeContent("everything he learned in school.\"");
// Nesting elements
// Nested elements are created by starting them within the
// current element. This element will have a "type" attribute.
// There are three ways to add attributes to an element. A
// single attribute can be added just by passing two Strings
// (one name and one value). Multiple attributes can either
// be passed by a Map or by an org.xml.sax.Attributes object.
xml.startElement("author", "type", "name");
xml.writeContent("Albert Einstein");
xml.endElement(); // author
// Empty elements
// Empty elements do not contain any content, but are immediately
// closed. As you will see here, you also don't need to worry
// about escaping attribute values. In this example the ampersand
// will be escaped by XMLWriter.
xml.startElement("category", "about", "Education & Life");
xml.endElement(); // category
// Close all elements
// Remember to always close all elements you have opened!
xml.endElement(); // quote
// End the document
// Do not forget to end what you've started.
xml.endDocument();
// That's all folks!
} catch (IOException ex) {
System.err.println("Failed writing an XML file");
ex.printStackTrace();
} finally {
if (xml != null) {
try { xml.close(); } catch (IOException ex) {}
}
}
}
}
The resulting XML file will look like this:
<?xml version="1.0" encoding="UTF-8"?> <!-- This is a jshred generated XML file --> <quote> "Education is what remains after one has forgotten everything he learned in school." <author type="name">Albert Einstein</author> <category about="Education & Life"/> </quote>