Java tutorial
//package com.java2s; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; public class Main { public static Element stringToElement(String str) { StringReader sr = new StringReader(str); try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(false); docBuilderFactory.setValidating(false); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); InputSource is = new InputSource(sr); // is.setEncoding("gb2312"); Document doc = docBuilder.parse(is); return doc.getDocumentElement(); } catch (Exception e) { e.printStackTrace(); return null; } } }