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 {
    JAXBContext jc = JAXBContext.newInstance(Person.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader(
            "<person><id>123</id><first-name>Tom</first-name><last-name>Smith</last-name></person>");
    Person person = (Person) unmarshaller.unmarshal(xml);

    System.out.println(person.id);
    System.out.println(person.firstName);
    System.out.println(person.lastName);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml1 = "<abc><name>hello</name></abc>";
    String xml2 = "<xyz><name>hello</name></xyz>";
    Unmarshaller unmarshaller = JAXBContext.newInstance(Foo.class).createUnmarshaller();
    Object o1 = unmarshaller.unmarshal(new StringReader(xml1));
    Object o2 = unmarshaller.unmarshal(new StringReader(xml2));
    System.out.println(o1);//from   w  w  w.j  av a  2s.c  o  m
    System.out.println(o2);
}

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("<main><bar>Hello World</bar></main>");
    Main foo = (Main) unmarshaller.unmarshal(xml);

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new StringReader(XML));
    String result = (String) xpath.evaluate("//@Build-Label", xml, XPathConstants.STRING);
    System.out.println(result);/*from ww  w  . j  a  v  a 2s. c  om*/
}

From source file:Main.java

public static void main(String arg[]) throws Exception {
    String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>";
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);//from  w ww.  j av  a 2  s  .  c o m
    NodeList nodes = doc.getElementsByTagName("x");
    System.out.println(nodes.getLength());
    List<String> valueList = new ArrayList<String>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        String name = element.getTextContent();
        // Element line = (Element) name.item(0);
        System.out.println("Name: " + name);
        valueList.add(name);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    // Unmarshal #1 = Default Unmarshal
    System.out.println("Unmarshal #1");
    Root root = (Root) unmarshaller.unmarshal(new StringReader(XML));
    marshaller.marshal(root, System.out);

    // Unmarshal #2 - Override Default ValidationEventHandler
    System.out.println("Unmarshal #2");
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override//from  www.  ja  v  a 2 s .  co m
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event.getMessage());
            return false;
        }
    });
    unmarshaller.unmarshal(new StringReader(XML));
}

From source file:Main.java

public static void main(String[] args) throws DocumentException {
    String newNode = "<node>value</node>";
    String text = "<root><given></given></root>";
    DOMDocumentFactory factory = new DOMDocumentFactory();
    SAXReader reader2 = new SAXReader();
    reader2.setDocumentFactory(factory);
    org.dom4j.Document document = reader2.read(new StringReader(text));
    Document newNodeDocument = reader2.read(new StringReader(newNode));

    Element givenNode = document.getRootElement().element("given");
    givenNode.add(newNodeDocument.getRootElement());

    org.dom4j.dom.DOMDocument w3cDoc = (DOMDocument) document;
    org.w3c.dom.Element e = w3cDoc.createElement("div");
    e.setAttribute("id", "someattr");

    w3cDoc.getDocumentElement().getFirstChild().insertBefore(e,
            w3cDoc.getDocumentElement().getElementsByTagName("node").item(0));
    System.out.println(document.asXML());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from  w ww  .  j ava2s . c  o m
    SAXParser parser = factory.newSAXParser();
    parser.parse(new InputSource(new StringReader(getXMLData())), new SampleOfXmlLocator());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Main.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    String code = "<book><title>Harry Potter</title></book>";
    StreamSource source = new StreamSource(new StringReader(code));
    JAXBElement<Main> jaxbElement = unmarshaller.unmarshal(source, Main.class);
}

From source file:Main.java

public static void main(String[] args) {
    String personJSONData = "  {" + "   \"name\": \"Jack\", " + "   \"age\" : 13, "
            + "   \"isMarried\" : false, " + "   \"address\": { " + "     \"street\": \"#1234, Main Street\", "
            + "     \"zipCode\": \"123456\" " + "   }, "
            + "   \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] " + " }";

    JsonReader reader = Json.createReader(new StringReader(personJSONData));

    JsonObject personObject = reader.readObject();

    reader.close();/*w  w  w  . ja  v a  2  s . c  om*/

    System.out.println("Name   : " + personObject.getString("name"));
    System.out.println("Age    : " + personObject.getInt("age"));
    System.out.println("Married: " + personObject.getBoolean("isMarried"));

    JsonObject addressObject = personObject.getJsonObject("address");
    System.out.println("Address: ");
    System.out.println(addressObject.getString("street"));
    System.out.println(addressObject.getString("zipCode"));

    System.out.println("Phone  : ");
    JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers");
    for (JsonValue jsonValue : phoneNumbersArray) {
        System.out.println(jsonValue.toString());
    }
}