Java XML DOM from String stringToDOM(String html)

Here you can find the source of stringToDOM(String html)

Description

string To DOM

License

Open Source License

Declaration

public static Document stringToDOM(String html) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.*;

public class Main {
    private static DocumentBuilder builder;

    public static Document stringToDOM(String html) {
        if (html.trim().startsWith("<?")) {
            int pos = html.indexOf("?>");
            if (pos > 0) {
                html = html.substring(pos + 2).trim();
            }/*from   ww  w .  j a v  a2  s  . c o  m*/
        }
        html = html.replace("&&", "&amp;&amp;");
        InputSource inputSource = new InputSource(new StringReader(html));

        try {
            Document domTree = builder.parse(inputSource);
            return domTree;
        } catch (SAXException e) {
            throw new RuntimeException("Couldn't parse the HTML oder XML code due to a SAXException", e);
        } catch (IOException e) {
            throw new RuntimeException("Couldn't parse the HTML oder XML code due to an IOException", e);
        }
    }
}

Related

  1. string2Dom(final String fileContent)
  2. string2Dom(String xml)
  3. stringToDom(String xmlSource)
  4. stringToDOM(String xmlString)
  5. stringToDOM(String xmlString)