Using the DocumentBuilderFactory

22 April 2008

In this post, I will prensent an example to show how to use the DocumentBuilderFactory.

First step is to impor the required classes.

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
 
// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
 
// DOM
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Now lets come tot he real thing. We have to obtain an instance of DocumentBuilderFactory. Then we have to configure the factory to handle validation and namespaces.

We also need a DocumentBuilder instance, which is created using DocumentBuilderFactory instance. Now we may start parsing and the resultant DOM Document object is handed off to a method that prints the DOM tree.

public class TestDOMParsing {
 
    public static void main(String[] args) {
        try {
            if (args.length != 1) {
                System.err.println ("Usage: java TestDOMParsing " +
                                    "[filename]");
                System.exit (1);
            }
 
            // Get Document Builder Factory
            DocumentBuilderFactory factory = 
                DocumentBuilderFactory.newInstance();
 
            // Turn on validation, and turn off namespaces
            factory.setValidating(true);
            factory.setNamespaceAware(false);
 
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File(args[0]));
 
            // Print the document from the DOM tree and
            //   feed it an initial indentation of nothing
            printNode(doc, "");
 
        } catch (ParserConfigurationException e) {
            System.out.println("The underlying parser does not " +
                               "support the requested features.");
        } catch (FactoryConfigurationError e) {
            System.out.println("Error occurred obtaining Document " +
                               "Builder Factory.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static void printNode(Node node, String indent)  {
        // print the DOM tree
    }
 
}

del.icio.us:Using the DocumentBuilderFactory  digg:Using the DocumentBuilderFactory  spurl:Using the DocumentBuilderFactory  wists:Using the DocumentBuilderFactory  simpy:Using the DocumentBuilderFactory  newsvine:Using the DocumentBuilderFactory  blinklist:Using the DocumentBuilderFactory  furl:Using the DocumentBuilderFactory  reddit:Using the DocumentBuilderFactory  fark:Using the DocumentBuilderFactory  blogmarks:Using the DocumentBuilderFactory  Y!:Using the DocumentBuilderFactory  smarking:Using the DocumentBuilderFactory  magnolia:Using the DocumentBuilderFactory  segnalo:Using the DocumentBuilderFactory  gifttagging:Using the DocumentBuilderFactory

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Using the DocumentBuilderFactory

Leave a Reply