Here you can find the source of createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema)
public static Document createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema) throws ParserConfigurationException, SAXException, IOException
//package com.java2s; // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Main { private static String defaultNameSpaces = " xmlns:gmi=\"http://www.isotc211.org/2005/gmi\" xmlns:gco=\"http://www.isotc211.org/2005/gco\" xmlns:gmd=\"http://www.isotc211.org/2005/gmd\" xmlns:gml=\"http://www.opengis.net/gml/3.2\" xmlns:gmx=\"http://www.isotc211.org/2005/gmx\" xmlns:ows=\"http://www.opengis.net/ows/2.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; public static Document createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema) throws ParserConfigurationException, SAXException, IOException { Document ret = null;// w w w. j a v a 2s . c om if (null == inputStreamXmlFragment) { throw new NullPointerException("input stream XML fragment is null"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); if (null != urlSchema) { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(urlSchema); factory.setSchema(schema); } DocumentBuilder docBuilder = factory.newDocumentBuilder(); ret = docBuilder.parse(inputStreamXmlFragment); inputStreamXmlFragment.close(); return ret; } public static Document createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment) throws ParserConfigurationException, SAXException, IOException { return createDocumentFromXmlFragment(inputStreamXmlFragment, null); } public static Document createDocumentFromXmlFragment(final String xmlFragment, final URL urlSchema) throws ParserConfigurationException, SAXException, IOException { if (null == xmlFragment) { throw new NullPointerException("XML fragment is null"); } StringBuilder xmlFragmentNs = new StringBuilder(xmlFragment); if (!hasNS(xmlFragment)) { xmlFragmentNs.insert(xmlFragmentNs.indexOf(">"), defaultNameSpaces); } return createDocumentFromXmlFragment(new ByteArrayInputStream(xmlFragmentNs.toString().getBytes()), urlSchema); } public static Document createDocumentFromXmlFragment(final String xmlFragment) throws ParserConfigurationException, SAXException, IOException { return createDocumentFromXmlFragment(xmlFragment, null); } public static boolean hasNS(final String xmlFragment) { boolean ret = false; if (null != xmlFragment) { ret = xmlFragment.contains("xmlns:"); } return ret; } }