Java Utililty Methods XML Document Create

List of utility methods to do XML Document Create

Description

The list of methods to do XML Document Create are organized into topic(s).

Method

DocumentcreateDocument(String xml)
convenience method to create an xml Document.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
doc.getDocumentElement().normalize();
return doc;
DocumentBuildercreateDocumentBuilder()
Builds new DocumentBuilder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(true);
dbf.setNamespaceAware(false);
try {
    return dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    throw new RuntimeException("Failed to create a document builder factory", e);
...
DocumentBuildercreateDocumentBuilder(boolean namespaces, boolean validating)
create Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaces);
factory.setValidating(validating);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder;
DocumentBuilderFactorycreateDocumentBuilderFactory()
Creates a new DocumentBuilderFactory instance.
return DocumentBuilderFactory.newInstance();
DocumentBuilderFactorycreateDocumentBuilderFactory(ClassLoader classLoader)
create Document Builder Factory
if (classLoader == null)
    classLoader = Thread.currentThread().getContextClassLoader();
return DocumentBuilderFactory.newInstance(DOCUMENT_BUILDER_FACTORY_IMPL, classLoader);
DocumentcreateDocumentFromElement(final Element element)
create Document From Element
Document ret = null;
if (null == element) {
    throw new NullPointerException("input DOM element is null");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
ret = factory.newDocumentBuilder().newDocument();
Node domNode = ret.importNode(element, true);
...
DocumentcreateDocumentFromFile(File file)
create Document From File
DocumentBuilderFactory factory = getDocumentBuilderFactory();
DocumentBuilder builder;
try {
    builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    throw new Exception(e);
return builder.parse(file);
...
DocumentcreateDocumentFromResult(final StreamResult result)
returns Document from StreamResult
Document document = null;
final String fileString = result.getWriter().toString();
try {
    final DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    final StringReader reader = new StringReader(fileString);
    final InputSource inputSource = new InputSource(reader);
    document = parser.parse(inputSource);
} catch (final SAXException e) {
...
DocumentcreateDocumentFromString(String str)
create Document From String
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new StringReader(str)));
return document;
DocumentcreateDocumentFromString(String xmlString)
create Document From String
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
ByteArrayInputStream byteIs = new ByteArrayInputStream(xmlString.getBytes());
Document doc = parser.parse(byteIs);
return doc;