Java XML Document Create createDocumentFromResult( final StreamResult result)

Here you can find the source of createDocumentFromResult( final StreamResult result)

Description

returns Document from StreamResult

License

Open Source License

Declaration

public static Document createDocumentFromResult(
        final StreamResult result) 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**/*from  w w  w .j  av a 2 s  . co m*/
     * returns Document from StreamResult
     */
    public static Document createDocumentFromResult(
            final StreamResult result) {
        Document document = null;
        final String fileString = result.getWriter().toString();
        try {
            final DocumentBuilder parser = DocumentBuilderFactory
                    .newInstance().newDocumentBuilder();
            final StringReader reader = new StringReader(fileString);
            final InputSource inputSource = new InputSource(reader);
            document = parser.parse(inputSource);
        } catch (final SAXException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
        }
        return document;
    }
}

Related

  1. createDocumentBuilder(boolean namespaces, boolean validating)
  2. createDocumentBuilderFactory()
  3. createDocumentBuilderFactory(ClassLoader classLoader)
  4. createDocumentFromElement(final Element element)
  5. createDocumentFromFile(File file)
  6. createDocumentFromString(String str)
  7. createDocumentFromString(String xmlString)
  8. createDocumentFromXml(String input)
  9. createDocumentFromXMLContent(String docContent)