Fibonacci with DOM3

import java.math.*;
import java.io.*;
import org.w3c.dom.*;
import org.w3c.dom.bootstrap.*;
import org.w3c.dom.ls.*;


public class FibonacciDOM3 {

  public static void main(String[] args) throws Exception {

      System.setProperty(DOMImplementationRegistry.PROPERTY,
        "org.apache.xerces.dom.DOMImplementationSourceImpl");
      DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
      DOMImplementation impl = registry.getDOMImplementation("XML 1.0 LS");
      if (impl == null) {
         System.err.println("Oops! Couln't find DOM3 implementation");   
         return;
      }
      Document fibonacci = impl.createDocument(null, "Fibonacci_Numbers", null );

      BigInteger low  = BigInteger.ZERO;
      BigInteger high = BigInteger.ONE;

      Element root = fibonacci.getDocumentElement();

      for (int i = 0; i <= 25; i++) {
        Element number = fibonacci.createElement("fibonacci");
        number.setAttribute("index", Integer.toString(i));
        Text text = fibonacci.createTextNode(low.toString());
        number.appendChild(text);
        root.appendChild(number);

        BigInteger temp = high;
        high = high.add(low);
        low = temp;
      }

      // Now that the document is created we need to *serialize* it
      DOMImplementationLS implls = (DOMImplementationLS) impl;
      LSSerializer serializer = implls.createLSSerializer();
      LSOutput output = implls.createLSOutput();
      output.setByteStream(new FileOutputStream("fibonacci_dom.xml"));
      
      serializer.write(fibonacci, output);

  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000-2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 11, 2004