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 unMarshal(String content, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    return unmarshaller.unmarshal(new StreamSource(new StringReader(content)), clazz).getValue();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshal(Class<T> beanClass, String xml) throws JAXBException {
    StringReader reader = new StringReader(xml);
    return (T) getUnmarshaller().unmarshal(reader);
}

From source file:Main.java

public static Document String2XML(String xmlStr) {
    try {/*  ww  w.  j  a v  a 2 s. c  o  m*/
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(is);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/** Parse a XML document from string */
public static Document parse(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 XMLStreamReader stringToStream(String content) throws IOException {

    //get Reader connected to XML input from somewhere..?
    Reader reader = new StringReader(content);
    try {//from w  ww  .  j av a 2  s  .  co  m
        return xiFactory.createXMLStreamReader(reader);
    } catch (XMLStreamException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static Document buildAndParseDocument(String in) {
    try {//from  w  w  w. j  a va 2 s . co  m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static boolean isValidXML(String xml) {
    try {//www  . j av  a 2s.c om
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Reader reader = null;
        try {
            reader = new StringReader(xml);
            builder.parse(new InputSource(reader));
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static Document load(String src) throws IOException, SAXException, ParserConfigurationException {
    Document doc = null;/*from w  w w  . j a  v  a  2s  . co m*/
    InputSource is = new InputSource(new StringReader(src));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    doc = docBuilder.parse(is);

    return doc;
}

From source file:Main.java

public static NodeList getXmlNodeSetForExpression(String expression, String is)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(is));
    return (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
}

From source file:Main.java

public static <T> T converyToJavaBean(String xmlStr, Class<T> c) {
    T t = null;//ww  w . ja v  a  2  s . com
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t;
}