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

/**
 * Parsing xml doc text as w3c dom/*w ww.  j  a v a2  s  . com*/
 *
 * @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

/**
 * // w w  w  .  j a v  a 2 s .c  om
 * @param xmlData
 * @return
 * @return
 * @return
 * @throws JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> T xmlStringToPojo(String xmlData, Class<T> targetClass) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(targetClass);
    StringReader reader = new StringReader(xmlData);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static Document parseXmlStringToDocument(String xmlString) {

    if (xmlString == null)
        return null;

    Document document;//from  w  w  w . ja v  a 2 s.  c o m

    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

/**
 * Write the provided String, line by line, in the provided OutputStream.
 *///from  www . j  av a2  s . c o  m
private static void writeStream(String toWrite, OutputStream out) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
    BufferedReader reader = new BufferedReader(new StringReader(toWrite));
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        writer.write(line);
    }
    reader.close();
    writer.close();
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {
    LinkedList result = new LinkedList();

    DOMParser parser = new DOMParser();
    try {//from ww  w  .  j  a  va 2  s  .c om
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++)
            if (nl.item(i).getNodeType() == 1)
                result.add(nl.item(i).getTextContent());
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {
    LinkedList result = new LinkedList();

    DOMParser parser = new DOMParser();
    try {/*from   w w  w  . ja v  a 2s  .  co m*/
        parser.parse(new InputSource(new StringReader(input)));
        Document doc = parser.getDocument();
        NodeList nl = doc.getChildNodes().item(0).getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; ++i)
            if (nl.item(i).getNodeType() == 1)
                result.add(nl.item(i).getTextContent());
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Document parse(String text) throws SAXException, IOException, ParserConfigurationException {
    return getDocumentBuilder().parse(new InputSource(new StringReader(text)));
}

From source file:Main.java

public static <T> T asObject(String xml, Class<T> clazz) {
    try {/*from   www. jav  a 2 s  .  com*/
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = null;
        unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static JAXBElement<?> xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element;
    try (StringReader reader = new StringReader(xml)) {
        element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
    }//from  w  w  w.ja  v  a 2 s. c  o m
    return element;
}

From source file:Main.java

/**
 * Formatte un XML//from  w  w w.  ja v  a  2  s .  c  o  m
 * */
public static String prettyFormat(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}