Here you can find the source of getRoot(String xml, String charset)
public static Element getRoot(String xml, String charset)
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main { public static Element getRoot(String xml, String charset) { try {/* ww w . ja v a 2 s. co m*/ InputStream in = null; DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); factory.setValidating(false); byte[] bytes = xml.getBytes(charset); in = new ByteArrayInputStream(bytes); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(in); Element root = dom.getDocumentElement(); if (root == null) { throw new RuntimeException("Invalid XML"); } return root; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }