Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.*; import javax.annotation.Nonnull; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Main { private static final Lock factoryLock = new ReentrantLock(); @Nonnull public static Document newDomDocument() throws IOException { try { DocumentBuilderFactory factory = newDocumentBuilderFactory(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.newDocument(); } catch (ParserConfigurationException e) { throw new IOException("Can't create new DOM-document.", e); } } public static DocumentBuilderFactory newDocumentBuilderFactory() { factoryLock.lock(); try { return DocumentBuilderFactory.newInstance(); } finally { factoryLock.unlock(); } } }