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 <T> T xml2Object(String xml, Class<T> clazz) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    StringReader reader = new StringReader(xml);
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static boolean isValidXml(String xml) {
    try {//from w  ww  .j  a  va  2s.c  o m
        StringReader reader = new StringReader(xml);
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        db.parse(new InputSource(reader));
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static Document readXmlFromString(String data) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(new org.xml.sax.InputSource(new StringReader(data)));
    return document;
}

From source file:Main.java

public static Object toObject(Class className, String strXml) {
    Object object = null;/*from  w w w .j a  v  a2  s.  c o  m*/
    StringReader reader = null;
    try {
        reader = new StringReader(strXml);
        JAXBContext context = JAXBContext.newInstance(className);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        object = unmarshaller.unmarshal(reader);
    } catch (Exception e) {
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }
    return object;
}

From source file:Main.java

private static void __toColoredHtml(String xmlText, HashMap<String, String> tagColorScheme, Writer writer)
        throws IOException {
    BufferedReader br = new BufferedReader(new StringReader(xmlText));

    String line;//from ww w .j  a v a2  s .co  m
    // String[] lines = xmlText.split("\r\n");
    while ((line = br.readLine()) != null) {
        __toColoredHtmlLine(line, tagColorScheme, writer);
    }
}

From source file:Main.java

public static Object xml2Bean(String xml, Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Unmarshaller um = context.createUnmarshaller();
    StringReader sr = new StringReader(xml);
    return um.unmarshal(sr);

}

From source file:Main.java

public static Document loadXml(String xml) {
    InputSource inputSource = new InputSource(new StringReader(xml));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {// w  w w  . j  a v a 2 s . com
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputSource);
        return doc;
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:Main.java

public static String format(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
    Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

    System.setProperty(DOMImplementationRegistry.PROPERTY,
            "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();

    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
    return writer.writeToString(document);
}

From source file:Main.java

public static Object xmlStringToObject(String xmlString, Class clazz) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = new StringReader(xmlString);
    return unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static <T> Object generateXML2Object(String str, Class cls) throws JAXBException {

    InputSource is;/*  w ww.  ja  v  a 2 s.com*/
    Object obj = null;

    is = new InputSource(new StringReader(str));
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    obj = cls.cast(jaxbUnmarshaller.unmarshal(is));

    return obj;
}