Java Utililty Methods XML DOM from String

List of utility methods to do XML DOM from String

Description

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

Method

Documentstring2Dom(final String fileContent)
string Dom
return DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new InputSource(new StringReader(fileContent)));
Documentstring2Dom(String xml)
string Dom
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(string2InputSource(xml));
    return doc;
} catch (Exception e) {
    System.out.println("caught exception while parsing: " + e);
...
DocumentstringToDOM(String html)
string To DOM
if (html.trim().startsWith("<?")) {
    int pos = html.indexOf("?>");
    if (pos > 0) {
        html = html.substring(pos + 2).trim();
html = html.replace("&&", "&amp;&amp;");
InputSource inputSource = new InputSource(new StringReader(html));
...
DocumentstringToDom(String xmlSource)
string To Dom
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
ElementstringToDOM(String xmlString)
Transforms a string representation to a DOM representation
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
Reader reader = new StringReader(xmlString);
InputSource src = new InputSource(reader);
Document domDoc = builder.parse(src);
return domDoc.getDocumentElement();
DocumentstringToDOM(String xmlString)
string To DOM
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xmlString)));
} catch (Throwable t) {
    throw new RuntimeException("Error while building document");