Java Utililty Methods XML Document from String

List of utility methods to do XML Document from String

Description

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

Method

DocumentgetDocumentFromBytes(byte[] data)
Creates an XML document object from a byte array.
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new ByteArrayInputStream(data));
return doc;
DocumentgetDocumentFromString(String text)
Returns a new document built from the input stream.
Document document;
try {
    DocumentBuilder docBuilder = getDocumentBuilder();
    InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
    document = docBuilder.parse(is);
} catch (Exception e) {
    e.printStackTrace();
    return null;
...
DocumentgetDocumentFromString(String xml)
get Document From String
Document retVal = null;
try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    retVal = db.parse(getInputSourceFromString(xml));
} catch (Exception ex) {
    ex.printStackTrace();
return retVal;
DocumentgetDocumentFromString(String xml)
get Document From String
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new ByteArrayInputStream(xml.getBytes()));
return document;
DocumentgetDocumentFromString(String xml)
get Document From String
InputSource source = new InputSource(new StringReader(xml));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);
document.normalize();
return document;
DocumentgetDocumentFromString(String xmlString)
Parses an XML string.
StringReader sr = new StringReader(xmlString);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return db.parse(new InputSource(sr));
DocumentgetDocumentFromString(String xmlString)
get Document From String
DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
    oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
            Boolean.FALSE);
} catch (IllegalArgumentException e) {
DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xmlString));
...
DocumentgetXmlDocument(final String container)
get Xml Document
final InputStream inputStream = new ByteArrayInputStream(container.getBytes());
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = factory.newDocumentBuilder();
return docBuilder.parse(inputStream);
DocumentgetXMLDocument(final String xml)
Parse string with XML content to org.w3c.dom.Document .
try {
    return getNewDocumentBuilder().parse(new InputSource(new StringReader(xml)));
} catch (IOException e) {
    throw new SAXException(e);
} catch (ParserConfigurationException e) {
    throw new SAXException(e);
DocumentloadDocument(String documentContent)
Method should be invoked only from unit tests
Class<?> documentClass = Class.forName("org.netbeans.editor.BaseDocument");
Constructor<?> documentConstructor = documentClass.getConstructor(boolean.class, String.class);
Document document = (Document) documentConstructor.newInstance(false, "text/xml");
document.insertString(0, documentContent, null);
return document;