Java XML Document Create getDocument(InputStream input)

Here you can find the source of getDocument(InputStream input)

Description

Get xml document, by stream

License

Open Source License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static Document getDocument(InputStream input) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Main {
    /**/*w  ww .j  a v a2  s  . co  m*/
     * Get xml document, by stream
     * 
     * @param input
     * @return
     * @throws Exception
     */
    public static Document getDocument(InputStream input) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(input);
        return doc;
    }

    /**
     * Get xml document, by xml string
     * 
     * @param xml
     * @return
     * @throws Exception
     */
    public static Document getDocument(String xml) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc;
    }
}

Related

  1. getDocument(File file)
  2. getDocument(File file)
  3. getDocument(File file)
  4. getDocument(final File file)
  5. getDocument(InputStream in)
  6. getDocument(InputStream inputStream)
  7. getDocument(InputStream inputStream)
  8. getDocument(InputStream xml)
  9. getDocument(InputStream xmlInputStream)