Here you can find the source of readDoc(Reader in)
Parameter | Description |
---|---|
in | The stream to read. |
Parameter | Description |
---|---|
TransformerException | A parsing problem. |
public static Document readDoc(Reader in) throws TransformerException
//package com.java2s; import org.w3c.dom.Document; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; import java.io.BufferedInputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Reader; public class Main { private static TransformerFactory transformerFactory = null; /**/*w w w. j a v a2s. c om*/ * Returns a document read from a file. * * @param xmlFile The file to read. * * @throws IOException An I/O problem. * @throws TransformerException A parsing problem. */ public static Document readDoc(File xmlFile) throws IOException, TransformerException { InputStream in = new BufferedInputStream(new FileInputStream(xmlFile)); return doReadDoc(in, new StreamSource(in)); } /** * Returns a document read from an {@link InputStream}. * * @param in The stream to read. * * @throws TransformerException A parsing problem. */ public static Document readDoc(InputStream in) throws TransformerException { return doReadDoc(in, new StreamSource(in)); } /** * Returns a document read from an {@link Reader}. * * @param in The stream to read. * * @throws TransformerException A parsing problem. */ public static Document readDoc(Reader in) throws TransformerException { return doReadDoc(in, new StreamSource(in)); } private static Document doReadDoc(Closeable in, StreamSource src) throws TransformerException { try { DOMResult res = new DOMResult(); getTransformer().transform(src, res); return (Document) res.getNode(); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { // do nothing } } } } private static Transformer getTransformer() throws TransformerConfigurationException { return getTransformerFactory().newTransformer(); } private static synchronized TransformerFactory getTransformerFactory() { if (transformerFactory == null) transformerFactory = TransformerFactory.newInstance(); return transformerFactory; } }