Java XML Transform Usage readDoc(Reader in)

Here you can find the source of readDoc(Reader in)

Description

Returns a document read from an Reader .

License

Open Source License

Parameter

Parameter Description
in The stream to read.

Exception

Parameter Description
TransformerException A parsing problem.

Declaration

public static Document readDoc(Reader in) throws TransformerException 

Method Source Code


//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;
    }
}

Related

  1. newSchema(Source[] schemas)
  2. paramsEqual(AlgorithmParameterSpec spec1, AlgorithmParameterSpec spec2)
  3. parse(File f)
  4. parse(final InputStream file, final ContentHandler handler)
  5. path(Element element)
  6. renderElement(Element theElem)
  7. safeToXml(Element element)
  8. streamSource(File file)
  9. toNonValidatingSAXSource(InputStream in)