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 void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
        s2 += s + "\n";
    in.close();/*from   ww w  . java2  s.c o m*/
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
        System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
        out1.println(lineCount++ + ": " + s);
    out1.close();
}

From source file:Main.java

public static void main(String arg[]) throws Exception {
    String xmlRecords = "<data><employee><name>A</name>" + "<title>Manager</title></employee></data>";

    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);/* w w w . ja v  a 2 s  . c o  m*/
    NodeList nodes = doc.getElementsByTagName("employee");

    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);

        NodeList name = element.getElementsByTagName("name");
        Element line = (Element) name.item(0);
        System.out.println("Name: " + getCharacterDataFromElement(line));

        NodeList title = element.getElementsByTagName("title");
        line = (Element) title.item(0);
        System.out.println("Title: " + getCharacterDataFromElement(line));
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    Map entityValues = new HashMap();
    getEntityValues(doc, entityValues);//from  w  w  w. j a  v  a 2  s  .  com

    NamedNodeMap entities = doc.getDoctype().getEntities();
    for (int i = 0; i < entities.getLength(); i++) {
        Entity entity = (Entity) entities.item(i);
        System.out.println(entity);
        String entityName = entity.getNodeName();
        System.out.println(entityName);
        String entityPublicId = entity.getPublicId();
        System.out.println(entityPublicId);
        String entitySystemId = entity.getSystemId();
        System.out.println(entitySystemId);
        Node entityValue = (Node) entityValues.get(entityName);
        System.out.println(entityValue);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);/* www .ja  v  a  2  s  .  c om*/
    SAXParser parser = factory.newSAXParser();
    SaxHandler handler = new SaxHandler();
    parser.parse(new InputSource(new StringReader(getXMLData())), handler);
}

From source file:InputOutputDemoString.java

public static void main(String[] a) throws Exception {

    //Read from a String s as if it were a text file:
    Reader r = new StringReader("abc");
    System.out.println("abc: " + (char) r.read() + (char) r.read() + (char) r.read());
    //Write to a StringBuffer as if it were a text file:
    Writer sw = new StringWriter();
    sw.write('d');
    sw.write('e');
    sw.write('f');
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new InputSource(new StringReader(xml)));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    Element userElement = (Element) xpath.evaluate("/users/user", document, XPathConstants.NODE);
    System.out.println(userElement.getAttribute("id"));
    System.out.println(userElement.getAttribute("firstname"));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;
    NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT,
            null, true);//w ww . ja va2  s .com

    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;

    TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null,
            true);/*from   w w w  .ja  v  a2s. c  om*/

    traverseLevel(walker, "");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Main.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<element>data</element>");
    Main myClass = (Main) unmarshaller.unmarshal(xml);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(myClass, System.out);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    DocumentTraversal traversal = (DocumentTraversal) document;

    NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL,
            new ItemFilter(), true);

    for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());
    }/* ww  w  . j  a  v  a 2 s.c  om*/
}