Return a new document, ready to populate : DOM Document « XML « Java

Return a new document, ready to populate

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

public class Utils {
  private static DocumentBuilderFactory dbf;

  private static Queue builders = new ConcurrentLinkedQueue();

  public static DocumentBuilder getBuilder() throws ParserConfigurationException {
    DocumentBuilder builder = (DocumentBuilder) builders.poll();
    if (builder == null) {
      if (dbf == null) {
        dbf = DocumentBuilderFactory.newInstance();
      builder = dbf.newDocumentBuilder();
    return builder;

  public static void releaseBuilder(DocumentBuilder builder) {

   * Return a new document, ready to populate.
   * @return
   * @throws ParserConfigurationException
  public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilder builder = getBuilder();
    Document doc = builder.newDocument();
    return doc;



Related examples in the same category

1.Copy an XML document
2.Create DOM Document out of string
3.Create Document with root QName
4.Create Empty DOM Document
5.Displays a DOM document in a tree control.
6.New Document From InputStream
7.New Document From String
8.load Document by element
9.load Document from InputStream
10.get Document Element from a file
11.Start a new XML Document
12.Document To String
13.Utility class to print out DOM
14.Read Xml from InputStream and return Document
15.Read Xml from Reader and return Document
16.Gets the owner document of a node.
17.Creates an element on the given document.
18.Loads a W3C XML document from a file.
19.Your Own XML Reader