Java tutorial
//package com.java2s; /* $This file is distributed under the terms of the license in /doc/license.txt$ */ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { private static DocumentBuilder parser; /** * @param xmlString * @return * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ public synchronized static Document parse(String xmlString) throws IOException, SAXException, ParserConfigurationException { StringReader reader = new StringReader(xmlString); InputSource inputSource = new InputSource(reader); return getDocumentBuilder().parse(inputSource); } /** * @param stream * @return * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ public synchronized static Document parse(InputStream stream) throws IOException, SAXException, ParserConfigurationException { return getDocumentBuilder().parse(stream); } /** * @return * @throws ParserConfigurationException */ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { if (parser == null) { // JPT: Remove xerces use DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setValidating(false); parser = documentBuilderFactory.newDocumentBuilder(); } return parser; } }