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 {
    String xml = "<xml xmlns:log='http://sample.com'><test log:writer='someWriter'/></xml>";
    StringReader xmlReader = new StringReader(xml);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from   www.  ja v a 2 s  .  com*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(xmlReader));

    Element currentNode = (Element) doc.getElementsByTagName("test").item(0);
    String attributeValue = currentNode.getAttributes().getNamedItemNS("http://sample.com", "writer")
            .getNodeValue();
    System.out.println("Attribute value is " + attributeValue);
    xmlReader.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<car><manufacturer>toyota</manufacturer></car>";
    String xpath = "/car/manufacturer";
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("toyota", xPath.evaluate(xpath, new InputSource(new StringReader(xml))));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputSource source = new InputSource(new StringReader("<root>\n" + "<field name='firstname'>\n"
            + "    <value>John</value>\n" + "</field>\n" + "<field name='lastname'>\n"
            + "    <value>Citizen</value>\n" + "</field>\n" + "<field name='DoB'>\n"
            + "    <value>01/01/1980</value>\n" + "</field>\n" + "<field name='Profession'>\n"
            + "    <value>Manager</value>\n" + "</field>\n" + "</root>"));

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);

    NodeList allFields = (NodeList) document.getElementsByTagName("field");

    Map<String, String> data = new HashMap<>();
    for (int i = 0; i < allFields.getLength(); i++) {
        Element field = (Element) allFields.item(i);
        String nameAttribute = field.getAttribute("name");
        Element child = (Element) field.getElementsByTagName("value").item(0);
        String value = child.getTextContent();
        data.put(nameAttribute, value);/* w ww .  j  av a2 s.c  om*/
    }

    for (Map.Entry field : data.entrySet()) {
        System.out.println(field.getKey() + ": " + field.getValue());
    }
}

From source file:Main.java

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

    StringReader xml = new StringReader("<Address><Name>Test</Name></Address>");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Address address = (Address) unmarshaller.unmarshal(xml);

    System.out.println(address.getPostalAddress().getState());

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    cdata.appendData("from java2s.com");
    System.out.println(cdataNode.getLength());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    cdata.setData("from java2s.com");
    System.out.println(cdataNode.substringData(2, 4));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    cdata.setData("from java2s.com");
    System.out.println(cdataNode);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    cdata.appendData("from java2s.com");
    cdata.deleteData(1, 2);/*  w  w w . j  a  v a 2 s  . c  o  m*/
    System.out.println(cdataNode);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = factory.newDocumentBuilder().parse(is);

    CDATASection cdataNode = doc.createCDATASection("");

    CharacterData cdata = cdataNode;

    cdata.appendData("from java2s.com");
    cdata.deleteData(1, 2);// w  w  w.j  av a2  s  .  co  m
    System.out.println(cdataNode.getData());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();

    Reader fileReader = new StringReader(getXMLData());
    XMLStreamReader reader = factory.createXMLStreamReader(fileReader);

    while (reader.hasNext()) {
        process(reader);//from   w w  w  . ja  va 2s . c  om
        reader.next();
    }
}