Here you can find the source of readString(String xml)
Parameter | Description |
---|---|
xml | is XML for a document |
public static Document readString(String xml)
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayInputStream; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.SAXParseException; public class Main { private static DocumentBuilderFactory mFactory = null; /**//from w w w . ja v a2 s . c om * @param xml is XML for a document * @return the Document contained in that XML */ public static Document readString(String xml) { ensureFactory(); try { DocumentBuilder builder = mFactory.newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); } catch (Exception e) { if (e instanceof SAXParseException) { SAXParseException spe = (SAXParseException) e; int l = spe.getLineNumber(); int c = spe.getColumnNumber(); StringTokenizer st = new StringTokenizer(xml, "\r\n"); String line = ""; for (int i = 0; (i < l) && st.hasMoreTokens(); i++) line = st.nextToken(); System.out.println("Line=" + l + ", column=" + c); System.out.println(line); for (int i = 0; i < c - 1; i++) System.out.print('-'); System.out.println('^'); } e.printStackTrace(); return null; } } private static synchronized void ensureFactory() { if (mFactory == null) { mFactory = DocumentBuilderFactory.newInstance(); //mFactory.setIgnoringComments(true); mFactory.setValidating(false); mFactory.setCoalescing(true); mFactory.setExpandEntityReferences(false); } } }