Chapter 3. Writing XML with Java

Table of Contents

Fibonacci Numbers
Writing XML
Better Coding Practices
Attributes
Producing Valid XML
Namespaces
Output Streams, Writers, and Encodings
A Simple XML-RPC Client
A Simple SOAP Client
Servlets
Summary

No one ever believes me when I tell them how easy it is to develop programs that write XML documents. In fact, writing a program to output an XML document is unbelievably trivial. It’s something an eight-year old typing BASIC in their first class at computer camp can do. In Java, it’s even easier than that due to Java’s strong Unicode support. You don’t need to know any special APIs like DOM or SAX or JDOM. All you have to know is how to System.out.println(). If you want to store your XML document in a file, you can use the FileOutputStream class instead. If you want to serve the document dynamically over a network, it helps to know something about servlets; but in the end it all reduces to writing bytes onto an output stream.

In this chapter, I’m going to develop a program that writes Fibonacci numbers into an XML document. I chose this example because the Fibonacci numbers are a well-known series that’s very easy to generate algorithmically so the examples will be nicely self-contained. However, the principles of XML you learn here will be much more broadly applicable to other, more complex systems. The key idea is that data arrives from some source, is encoded in XML, and is then output. Where the input comes from, whether an algorithm, a file, a network socket, user input, or some other source, really doesn’t concern us here.

I’m going to show you how to use classes you’re already familiar with like OutputStreamWriter, String, and HTTPServlet to generate XML documents. I am going to beat this idea into the ground until you are absolutely convinced that there is nothing special about XML documents that requires any fancy tricks to produce them. Once you’ve finished this chapter you’ll be thoroughly immunized against the snake-oil peddlers who want to sell you multi-hundred thousand dollar software to do what you can do with your existing systems for free.

Fibonacci Numbers

As far as we know, the Fibonacci series was first discovered by Leonardo of Pisa around 1200 C.E. Leonardo was trying to answer the question, “Quot paria coniculorum in uno anno ex uno pario germinatur?”, or, in English, “How many pairs of rabbits are born in one year from one pair?” To solve his problem, Leonardo estimated that rabbits have a one month gestation period, and can first mate at the age of one month, so that each female rabbit has its first litter at two months. He made the simplifying assumption that each litter consisted of exactly one male and one female.

Leonardo begins with one pair of baby rabbits, a male and a female. At the end of the first month, these two have reached puberty and mate. There’s still one pair of rabbits. At the end of the second month, the female gives birth to a new pair of rabbits. There are now two pairs of rabbits, one pair of adults and one pair of babies. The adult pair mates again, so that they will produce one more pair at the end of the third month, at which point there are now three pairs of rabbits. One of these pairs has just been born, but the other two are old enough to mate, which, being rabbits, they do. At the end of the third month, two of the three pairs have babies producing five pairs of rabbits. Meanwhile all rabbits born in previous months mate, so that at the end of the fourth month there will be three more pairs of rabbits. Leonardo realized that the number of pairs at the end of each month was the sum of the number of pairs the preceding month and the number of pairs the month before that. The rabbits don’t simply double in population each month because it takes two months before a rabbit can have its first litter. Nonetheless, the numbers do grow only slightly more slowly than exponentially; and the process continues indefinitely, at least until you run out of rabbit food or the rabbits take over the world, whichever comes first. The number of pairs each month—1, 1, 2, 3, 5, 8, 13, ...— has come to be known as the Fibonacci series after Leonardo’s Latin nickname, Fibonacci (short for filius Bonacci, son of Bonacci).

The rabbits aren’t so important, but the math is. Each integer in the series is formed by the sum of the two previous integers. The first two integers in the series are 1 and 1.[1] The Fibonacci series turns up in some very unexpected places including decimal expansions of π, the golden ratio, the Mandelbrot set, the number of petals on many flowers, the number of spirals on pine cones and pineapples, the number of chambers in a Nautilus shell, and many more.

It’s very easy to calculate the Fibonacci sequence by computer. A simple for loop will do. For example, this code fragment prints the first 40 Fibonacci numbers:

    int low = 1;
    int high = 1;
    for (int i = 1; i <= 40; i++) {
      System.out.println(low);
      int temp = high;
      high = high+low;
      low = temp;
    }

However, the Fibonacci numbers do grow very large very quickly, and exceed the bounds of an int shortly before the fiftieth generation. Consequently, it’s better to use the java.math.BigInteger class instead, as Example 3.1 demonstrates:

Example 3.1. A program that calculates the Fibonacci numbers

import java.math.BigInteger;

public class FibonacciNumbers {

  public static void main(String[] args) {
  
    BigInteger low  = BigInteger.ONE;
    BigInteger high = BigInteger.ONE;
    for (int i = 1; i <= 10; i++) {
      System.out.println(low);
      BigInteger temp = high;
      high = high.add(low);
      low = temp;
    }
    
  }

}

When run, this program produces the first ten Fibonacci numbers:

C:\XMLJAVA>java FibonacciNumbers
1
1
2
3
5
8
13
21
34
55

It would be straightforward to read the number of Fibonacci numbers to generate from the command line. However, since user interfaces aren’t the focus of this book, I don’t want to obscure the important parts with too much extraneous fluff.



[1] In some variations, the first two integers are 0 and 1. Aside from the initial zero, this produces the same series.


Copyright 2001, 2002 Elliotte Rusty Haroldelharo@metalab.unc.eduLast Modified January 21, 2002
Up To Cafe con Leche