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 loadXMLFromString(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

public static String format(String input, int indent) {
    try {//from w w  w.j a  va  2s. c o m
        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 (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document stringToDoc(final String xml) throws Exception {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

public static String prettyFormat(String input) {
    try {/*from  ww  w .  j av  a 2 s . c om*/
        final InputSource src = new InputSource(new StringReader(input));
        final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src)
                .getDocumentElement();
        final Boolean keepDeclaration = Boolean.valueOf(input.startsWith("<?xml"));
        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();

        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
        writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.

        return writer.writeToString(document);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xml2obj(String xml, Class<T> type) {
    T obj = null;/*from   ww w  . j  ava2 s  .c  o  m*/
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T convert2JavaBean(String xml, Class<T> c) {
    T t = null;//ww  w  .ja v a  2 s  .  c o  m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
        return t;
    }
    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;/*from  w w w  .  j  a v  a 2  s  . c  o m*/
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object convertXmlStringToObject(String xml, Class clazz) {
    Object object = null;//from   ww  w  .  j  a  v  a2 s. c  om
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        StringReader xmlReader = new StringReader(xml);
        object = unmarshaller.unmarshal(xmlReader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return object;
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "restriction" })
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*from ww  w.j  a v  a  2  s.  c om*/
        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 Document stringToXML(String xmlString) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from  ww  w.  ja  v a 2  s .  c o m
    Document document = null;
    try {
        document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}