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 buildAndParseDocument(String in) {
    try {//from  w w  w . j  a  v  a2 s.  c  o m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document load(String src) throws IOException, SAXException, ParserConfigurationException {
    Document doc = null;// w  w  w  . ja  v a  2 s  . c o  m
    InputSource is = new InputSource(new StringReader(src));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    doc = docBuilder.parse(is);

    return doc;
}

From source file:Main.java

public static NodeList getXmlNodeSetForExpression(String expression, String is)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(is));
    return (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList evalXpath(InputStream xmlStream, String path) throws XPathExpressionException {
    InputSource inXML = new InputSource(xmlStream);
    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();
    XPathExpression expr = xpath.compile(path);
    Object result = expr.evaluate(inXML, XPathConstants.NODESET);
    return (NodeList) result;
}

From source file:Main.java

public static Document createDocument(File file) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new FileInputStream(file)));
    return doc;//from  w ww  .ja  v  a  2s  .c  o  m
}

From source file:Main.java

public static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;/*from www  .ja  va 2s .  c  om*/
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document stringToDOM(String xmlString) {
    try {//from w ww .j  ava  2  s  . c  o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (Throwable t) {
        throw new RuntimeException("Error while building document");
    }
}

From source file:Main.java

/**
 * Parsing xml doc text as w3c dom//w  w w .  j a va 2s .c  om
 *
 * @param xmlString xml doc as string
 * @return W3C DOM
 */
public static Document parseNSAware(String xmlString) {
    try {
        InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(xmlInputSource);
    } catch (Exception ex) {
        throw new RuntimeException("Error when parsing Xml doc", ex);
    }
}

From source file:Main.java

public static Document parseXmlStringToDocument(String xmlString) {

    if (xmlString == null)
        return null;

    Document document;/*from  ww w  .j  a  va2s .  com*/

    try {

        InputSource source = new InputSource(new StringReader(xmlString));

        document = documentBuilder.parse(source);
    } catch (IOException ex) {

        throw new RuntimeException("Failed to parse input stream due to I/O errors: " + ex.getMessage(), ex);
    } catch (SAXException ex) {

        throw new RuntimeException("Failed to parse input stream due to SAX errors: " + ex.getMessage(), ex);
    }

    return document;
}

From source file:Main.java

public static Document createDocument(InputStream input) {
    DOMParser parser = null;//from  ww w.j a  v  a2s  .  c  o  m
    try {
        parser = getLargeParser(input.available());
        parser.parse(new InputSource(new FilterInputStream(input) {
            public void close() {
                // disable close. Because createDocument calls close, but it shoudn't do it.
            }
        }));
        return parser.getDocument();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}