Example usage for java.io StringReader StringReader

List of usage examples for java.io StringReader StringReader

Introduction

In this page you can find the example usage for java.io StringReader StringReader.

Prototype

public StringReader(String s) 

Source Link

Document

Creates a new string reader.

Usage

From source file:Main.java

/**
 * Convert String to an XML document./*from  ww  w  .j  a va2 s.c o m*/
 */
public static Document stringToXML(String xml) {
    try {
        return parseReader(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document getW3CDom(String xmlStr) throws ParserConfigurationException, SAXException, IOException {
    StringReader sr = new StringReader(xmlStr);
    InputSource is = new InputSource(sr);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    return doc;/*  ww  w  . j  a  v a  2  s. c  o m*/
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Object unmarshal(String xmlBody, Class objectClass) {
    try {//from  ww  w  .  j a  v a  2  s. c  o m
        JAXBContext context = JAXBContext.newInstance(objectClass);
        Unmarshaller u = context.createUnmarshaller();
        return u.unmarshal(new StreamSource(new StringReader(xmlBody)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from w  w w . jav  a 2  s  . com
    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 String getNodeCount(String nodeName, String xmlString) {
    String response = "";
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {//from  www.j ava 2  s.  c  o m
        XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")");
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlString));
        response = xPathExpression.evaluate(is);

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

    return response;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xml2Bean(String xml, Class<?> clazz) throws Exception {
    StringReader reader = null;/*from  w w w  .  j a  v a 2 s  . c  o m*/
    try {
        JAXBContext jc = JAXBContext.newInstance(new Class[] { clazz });
        Unmarshaller m = jc.createUnmarshaller();
        reader = new StringReader(xml);
        return (T) m.unmarshal(reader);
    } catch (Exception e) {
    } finally {
        close(reader);
    }
    return null;
}

From source file:Main.java

public static Document stringToDOM(String xmlString) {
    try {/*ww  w  .  ja v a  2s  . 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

public static Document StringToxml(String resultData) {

    StringReader sr = new StringReader(resultData);
    InputSource is = new InputSource(sr);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    Document doc = null;/*from  w ww . j av a 2s  .c  om*/
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {

        Log.e(DEBUG_TAG, "the StringToxml method ParserConfigurationException erro ");
    }
    try {
        doc = builder.parse(is);

    } catch (SAXException e) {

        Log.e(DEBUG_TAG, "the StringToxml method SAXException erro ");

    } catch (IOException e) {
        Log.e(DEBUG_TAG, "the StringToxml method IOException erro ");
    }
    return doc;
}

From source file:Main.java

public static Document domFromString(String in) {
    try {//from  w  w w  . j a v a  2  s  . c  o  m
        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 documentFromString(String xml) throws SAXException, IOException {

    Reader reader = new StringReader(xml);
    InputSource source = new InputSource(reader);
    DocumentBuilder db = newDocumentBuilder(false);
    return db.parse(source);
}