Creating XML Documents with Java

Because an XML document is simply a text file (or a stream of text), you can create one just by using a PrintWriter or even System.out.println. Listing 22.4 shows a simple Java program that prints out an XML document.

Code Listing 22.4. Source Code for PrintXML.java
package usingj2ee.xml;

public class PrintXML
{
    public static void main(String[] args)
    {
        System.out.println("<?xml version=\"1.0\"?>");
        System.out.println("<greeting>");
        System.out.println("   Hello XML World!");
        System.out.println("</greeting>");
    }
}

Obviously, there's nothing to printing out an XML file. Now, from a Java Server Page, you have one extra challenge. The content type for an XML page is different from the content type for an HTML page. ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.