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 LinkedList<String> uc_unserialize(String input) {

    LinkedList<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {//ww w .  j  av  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() == Document.ELEMENT_NODE)
                result.add(nl.item(i).getNodeValue());
        }
    } 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<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {//w w w. j a  v a 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() == Document.ELEMENT_NODE)
                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 convertStringToDocument(String domStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from w  w w.  ja va  2  s . c o  m
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(domStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static LinkedList<String> uc_unserialize(String input) {

    LinkedList<String> result = new LinkedList<String>();

    DOMParser parser = new DOMParser();
    try {//w  w w. j av a 2s  . c  o 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() == Document.ELEMENT_NODE)
                result.add(nl.item(i).getFirstChild().getNodeValue());
        }
    } catch (SAXException e) {

    } catch (IOException e) {

    }
    return result;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {//from w w w.  ja  v a2 s  .  com
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

public final static void writeSimpleElementCDATALine(Writer w, int deep, String element, String content)
        throws IOException {
    writeSimpleElementCDATALine(w, deep, element, new StringReader(content));
}

From source file:Main.java

/**
 * Forms an <code>InputSource</code> for parsing XML documents
 * from a string. Returns <code>null</code> if any of the exceptions
 * are thrown.//from   w w w.  j  a va2  s . co m
 * 
 * @param xml String with the XML document.
 * @return An <code>InputSource</code> representation.
 */
public static InputSource getInputSourceFromString(String xml) {
    InputSource retVal = null;
    try {
        retVal = new InputSource(new StringReader(xml));
    } catch (Exception ex) {
    }
    return retVal;
}

From source file:Main.java

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

From source file:Main.java

public static String getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {//w  w w. j  av  a  2s .  c om
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

public static Document readFileAsDoc(String xml) throws SAXException, IOException, Exception {
    org.w3c.dom.Document doc = null;
    StringReader sr = new StringReader(xml);
    DocumentBuilder bldr = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    doc = bldr.parse(new InputSource(sr));

    return doc;/*  w w w  .j a va  2  s . c  o  m*/
}