Java XML Parse InputSource parseXML(InputSource is)

Here you can find the source of parseXML(InputSource is)

Description

Parses an input source and generates an XML document.

License

LGPL

Parameter

Parameter Description
is An input source to an XML source.

Return

An XML doucument.

Declaration

public static Document parseXML(InputSource is) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.*;
import java.net.URL;

public class Main {
    /**// www.ja  va 2  s.  c om
     * Parses an input source and generates an XML document.
     * @param is An input source to an XML source.
     * @return An XML doucument.
     */
    public static Document parseXML(InputSource is) {
        try {
            Document doc = null;
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            doc = documentBuilder.parse(is);
            return doc;
        } catch (Exception e) {
            throw new RuntimeException("Failed to parse XML source", e);
        }
    }

    /**
     * Parses an input stream and generates an XML document.
     * @param is An input stream to an XML source.
     * @return An XML doucument.
     */
    public static Document parseXML(InputStream is) {
        return parseXML(new InputSource(is));
    }

    /**
     * Parses a file and generates an XML document.
     * @param file
     * @return An XML doucument.
     */
    public static Document parseXML(File file) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            return parseXML(fis);
        } catch (Exception e) {
            throw new RuntimeException("Failed to open XML file:" + file, e);
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Parses the input stream of a URL and generates an XML document.
     * @param xmlUrl The URL of the XML document.
     * @return The parsed document.
     */
    public static Document parseXML(URL xmlUrl) {
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = xmlUrl.openConnection().getInputStream();
            bis = new BufferedInputStream(is);
            return parseXML(bis);
        } catch (Exception e) {
            throw new RuntimeException("Failed to read XML URL:" + xmlUrl, e);
        } finally {
            try {
                bis.close();
            } catch (Exception e) {
            }
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Parses an XML string and generates an XML document.
     * @param xml The XML to parse
     * @return An XML doucument.
     */
    public static Document parseXML(CharSequence xml) {
        StringReader sr = new StringReader(xml.toString());
        return parseXML(new InputSource(sr));
    }
}

Related

  1. parse(final InputSource is, final Class loader)
  2. parse(InputSource in)
  3. parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver)
  4. parse(InputSource source)