Example usage for org.xml.sax InputSource InputSource

List of usage examples for org.xml.sax InputSource InputSource

Introduction

In this page you can find the example usage for org.xml.sax InputSource InputSource.

Prototype

public InputSource(Reader characterStream) 

Source Link

Document

Create a new input source with a character stream.

Usage

From source file:Main.java

public static Document loadXML(InputStream in) {
    try {/* w w w  .  j  a v a2 s. co  m*/
        if (in != null)
            return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new InputStreamReader(in, "UTF-8")));
    } catch (SAXException | IOException | ParserConfigurationException ex) {
    }
    return null;
}

From source file:Main.java

/**
 * Grab the Document found at the specified URL.
 * /*from www .  j  a  v a 2s .  c  om*/
 * @param ref the URL location of an XML document.
 * @return a Document loaded from the specified URL.
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document parse(final String ref) throws SAXException, IOException, ParserConfigurationException {
    final String protocol = ref.split(":", 2)[0];
    return parse(protocol, new InputSource(ref));
}

From source file:Main.java

/** 
 * Return a Node corresponding to the input XML string representation.
 * @param xmlFile XML file.//  w  w  w  .  ja  va2s.c  o m
 * @return An instance of Node corresponding to the input XML string representation.
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException 
 */
public static Node getXMLNode(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    InputSource inputSource = new InputSource(new BufferedInputStream(new FileInputStream(xmlFile)));
    return (getXMLNode(inputSource));
}

From source file:Main.java

public static Document string2Dom(final String fileContent)
        throws SAXException, IOException, ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(fileContent)));
}

From source file:Main.java

public static Document parseDoc(final InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    try {/*  ww  w  .jav a2  s .  co m*/
        BufferedInputStream in = new BufferedInputStream(is);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(in);
        return builder.parse(source);
    } finally {
        is.close();
    }
}

From source file:de.nava.informa.parsers.OPMLParser.java

public static Collection parse(URL aURL) throws IOException, ParseException {
    return parse(new InputSource(aURL.toExternalForm()), aURL);
}

From source file:Main.java

public static String findInXml(String xml, String xpath) {
    try {//from  www  .j  a  v  a  2 s.  c o  m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                //                    if (systemId.contains("foo.dtd")) {
                return new InputSource(new StringReader(""));
                //                    } else {
                //                        return null;
                //                    }
            }
        });
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPathExpression expr = xPathfactory.newXPath().compile(xpath);
        return (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:Main.java

/**
 * /*from   w  ww.ja va2  s  .c  om*/
 * <B>Purpose:</B> Parses the String to an XML Document Object
 * 
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xml)));

}

From source file:Main.java

public static Document stringToDom(String xmlSource)
        throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xmlSource)));
}

From source file:Main.java

/**
 * Liefert das {@link Document} aus dem Stream.
 * //from www  .ja  va  2  s  . c o  m
 * @param inputStream {@link InputStream}
 * @return {@link Document}
 * @throws Exception Falls was schief geht.
 */
public static Document getDocument(final InputStream inputStream) throws Exception {
    InputStream is = inputStream;

    if (!(is instanceof BufferedInputStream)) {
        is = new BufferedInputStream(is, 8192 * 4);
    }

    return getDocument(new InputSource(is));
}