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() 

Source Link

Document

Zero-argument default constructor.

Usage

From source file:Main.java

public static Document domFromString(String in) {
    try {//from w w  w.j  a va  2  s.  c om
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException ex) {
        throw new RuntimeException("Unable to parse xml.", ex);
    }
}

From source file:Main.java

public static Document xmlFromString(String xml) {
    Document doc;//from  w  w w.java 2 s.  c  o m

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

From source file:Main.java

public static Document parseTheXml(String thePage)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(thePage));

    return db.parse(is);
    //build dat DOM document!

}

From source file:Main.java

public static Vector<HashMap> xmltoVector222(String xmlFile, String xpath) {
    try {/*from w  ww . j a  v a2 s.c o m*/
        if (!new File(xmlFile).exists()) {
            return new Vector<HashMap>();
        }
        File xmlDocument = new File(xmlFile);
        InputSource inputSource = new InputSource();
        return xmlToVector222(new FileInputStream(xmlDocument), xpath);
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector<HashMap>();
    }
}

From source file:Main.java

static public Document getDomElement(String xml) {
    Document doc = null;/*from w  ww. j  ava2  s. c o  m*/
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();

        //Set the input to our xml string
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }

    //return DOM
    return doc;
}

From source file:Main.java

public static Map handleXMLResponse(HttpResponse response) {
    Map<String, String> oauthResponse = new HashMap<String, String>();
    try {// ww  w  .  j  a  v  a  2 s  .  co  m

        String xmlString = EntityUtils.toString(response.getEntity());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlString));
        Document doc = db.parse(inStream);

        System.out.println("********** XML Response Received **********");
        parseXMLDoc(null, doc, oauthResponse);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Exception occurred while parsing XML response");
    }
    return oauthResponse;
}

From source file:Main.java

/**
 * This method will Read the XML and act accordingly
 *
 * @param xmlString - the XML String//w  w w .  jav a 2 s  .c om
 * @return the list of elements within the XML
 */
public static Document readXML(String xmlString)
        throws SAXParseException, SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputSource xmlStream = new InputSource();
    xmlStream.setCharacterStream(new StringReader(xmlString));
    return dBuilder.parse(xmlStream);
}

From source file:Main.java

public static Document parseXml(String s) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilder builder = getBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(s));
    Document ret = builder.parse(is);
    return ret;/*from w  ww . ja v  a2  s. co m*/
}

From source file:Main.java

/**
 * Reads an xml string into XML Document.
 * /*from  ww  w .j  av a  2s .  c  o  m*/
 * @param xmlStr String containing xml
 * @return xml Document
 * @throws Exception
 */
public static Document readXmlString(String xmlStr) throws Exception {

    Document doc = null;

    try {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlStr));
        doc = builder.parse(inStream);
    } catch (ParserConfigurationException e) {
        throw new Exception(e.getMessage(), e);
    } catch (SAXException e) {
        throw new Exception(e.getMessage(), e);
    } catch (IOException e) {
        throw new Exception(e.getMessage(), e);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    }

    return doc;

}

From source file:Main.java

/**
 * DOM Parser method. //  www .j  a  va2s. c o m
 * Converts the input xmlString into List<String> of all the text in the nodes named nodeName.
 * The memory footprint is small enough when we deal with less than 200 tweets.
 * 
 * @param xmlString
 *            The xml string
 * @param nodeName
 *            the name of the node, we are intersted in (e.g. "text")
 * @param trimString
 *            if we want to trim a particular pattern from every node's text, we pass it as trimString, e.g. "(https?://[^\\s]+)"
 * @return List of all the text strings from every node named nodeName. trimString text is trimmed from the result strings.
 */
public static List<String> parseNodesFromXml(String xmlString, String nodeName, String trimString) {
    List<String> nodeTexts = new ArrayList<String>();

    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource inputSource = new InputSource();
        inputSource.setCharacterStream(new StringReader(xmlString));

        try {
            Document document = db.parse(inputSource);
            NodeList nodes = document.getElementsByTagName(nodeName);
            for (int i = 0; i < nodes.getLength(); i++) {
                String nodeText = nodes.item(i).getTextContent();
                String trimmedNodeText = trimStringFromText(trimString, nodeText);
                nodeTexts.add(trimmedNodeText);
            }

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return nodeTexts;
}