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

@SuppressWarnings("unchecked")
public static <T> T unmarshaller(String xml, Class<?> T) {
    JAXBContext jc;/* ww  w .j  a va2s.c  om*/
    Unmarshaller unmarshaller;
    Object o = null;
    try {
        jc = JAXBContext.newInstance(T);
        unmarshaller = jc.createUnmarshaller();
        o = unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return (T) o;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {//from www .ja  v a  2s.c  om
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static void stringToResult(String source, Result result) throws IOException {

    try {/*from  ww w.  ja  v  a 2 s .com*/
        Transformer trans = transFactory.newTransformer();
        StringReader reader = new StringReader(source);
        trans.transform(new StreamSource(reader), result);
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static List<String> xPathEvalList(String content, String expression) throws XPathExpressionException {
    List<String> retValues = new ArrayList<String>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(content));
    NodeList ox = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
    for (int i = 0; i < ox.getLength(); i++) {
        retValues.add(ox.item(i).getNodeValue());
    }/*from  w w w .  j  a  v a 2  s.  c o  m*/

    return retValues;
}

From source file:Main.java

private static Document parseXmlFile(String in) {
    try {// w  w  w  . jav  a2 s  . c  om
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document xmlFromString(String xml) {
    Document doc;/*from w w  w .  j  a va 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 String transform(String xslt, String xml, String charsetName) throws TransformerException {
    return transform(new StreamSource(new StringReader(xslt)), new StreamSource(new StringReader(xml)),
            charsetName);//from ww w  .  j a  va2 s .com
}

From source file:Main.java

public static Object unmarshal(String xml, Class<?> classObj) {
    Object obj;//from   w  w  w .  java2 s .  c  o m
    try {
        JAXBContext jaxbContext = getJAXBContext(classObj);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        obj = unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document parseXml(String xml) {
    try {// ww w .jav a  2  s  . c  o  m
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        setUpSecurity(dbFactory);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static List<Node> xPathEvalListNodes(String content, String expression) throws XPathExpressionException {
    List<Node> retValues = new ArrayList<Node>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(content));
    NodeList ox = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
    for (int i = 0; i < ox.getLength(); i++) {
        retValues.add(ox.item(i));//from  www .  j av a2s  .  c o m
    }

    return retValues;
}