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

public static Document parse(String xml) throws Exception {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

public static Object xmlToObject(Class clazz, String xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller um = context.createUnmarshaller();
    return um.unmarshal(new StringReader(xml));
}

From source file:Main.java

public static <T> T fromXML(String xml, Class<T> valueType) {
    try {//w w  w .  j ava 2  s .  c o  m
        JAXBContext context = JAXBContext.newInstance(valueType);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static <T> T xml2Obj(String xmlString, Class<T> clazz) throws RuntimeException {
    T c = null;/* w ww .ja v  a2 s .co m*/
    Reader reader = null;
    try {
        reader = new StringReader(xmlString);
        Unmarshaller unMarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
        c = (T) unMarshaller.unmarshal(reader);
    } catch (JAXBException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return c;
}

From source file:Main.java

private static void __htmlTextToPlainText(String text, Writer writer) throws IOException {
    StringReader sr = new StringReader(text);
    char[] chunk = new char[1024 * 64];

    while (true) {
        int charsRead = sr.read(chunk, 0, chunk.length);
        if (charsRead <= 0)
            break;

        int lastPos = 0;
        for (int i = 0; i < chunk.length; i++) {
            if (chunk[i] == '<') {
                writer.write(chunk, lastPos, i - lastPos);
                writer.write("&lt;");
                lastPos = i + 1;//from   w  w  w  . j  a  v a2 s .  com
            } else if (chunk[i] == '>') {
                writer.write(chunk, lastPos, i - lastPos);
                writer.write("&gt;");
                lastPos = i + 1;
            }
        }
        if (lastPos < chunk.length) {
            writer.write(chunk, lastPos, chunk.length - lastPos);
        }
    }
}

From source file:Main.java

public static Document load(String xml) throws Exception {
    DocumentBuilder builder = getDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}

From source file:Main.java

public static XMLStreamReader buildXmlStreamReader(String xml) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    return factory.createXMLStreamReader(new StringReader(xml));
}

From source file:Main.java

public static NodeList extractNodeList(XPath xpath, String nodePath, String xmlString) {
    InputSource inputSource = new InputSource(new StringReader(xmlString));

    try {//from  w w w.  j  av  a  2 s. com
        return (NodeList) xpath.evaluate(nodePath, inputSource, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:Main.java

public static <T> T unmarshal(JAXBContext context, Class<T> clazz, String source) throws JAXBException {

    if (source == null) {
        return null;
    }//from   ww  w .  j  a  v a  2 s.c o m

    StringReader reader = new StringReader(source);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
    }
    Unmarshaller un = context.createUnmarshaller();
    return (T) un.unmarshal(reader);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*from w w w  .  j  a v a 2  s  .co m*/
        JAXBContext context = JAXBContext.newInstance(valueType);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}