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 {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;//from   w  w  w  . j  a  va  2s  .  c o m
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();
    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyTextHandler());

    StringReader sr = new StringReader(
            "<folks><person><phone>502 555-2192</phone><name>B, M</name></person></folks>");

    //InputSource is = new InputSource("xmlFileName.xml");
    InputSource is = new InputSource(sr);
    reader.parse(is);
}

From source file:Main.java

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

    String xmlSample = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><nested>hello</nested></tag>";
    String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"xml\" version=\"1.0\" indent=\"yes\"/><xsl:template match=\"node()|@*\"><xsl:copy><xsl:apply-templates select=\"node()|@*\"/></xsl:copy></xsl:template></xsl:stylesheet>";

    TransformerFactory factory = TransformerFactory.newInstance();

    Source xslSource = new StreamSource(new StringReader(stylesheet));
    Transformer transformer = factory.newTransformer(xslSource);

    Source source = new StreamSource(new StringReader(xmlSample));
    Result result = new StreamResult(System.out);

    transformer.transform(source, result);

}

From source file:MainClass.java

static public void main(String[] arg) {
    boolean validate = false;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);/*from  w w w.j av  a 2  s. co  m*/
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }

    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:Main.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);/*w ww . j  a va2  s . c  om*/
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    // Parse the input to produce a parse tree with its root
    // in the form of a Document object
    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(getXMLData()));
        doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
    dump(doc);
}

From source file:Main.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);// ww  w .j  a v  a 2s  .c o  m
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();

        InputSource is = new InputSource(new StringReader(getXMLData()));
        doc = builder.parse(is);

        write(doc);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<Employees>"
            + "  <Employee emplid='1' type='admin'>" + "    <firstname/>" + "    <lastname>A</lastname>"
            + "    <age>30</age>" + "    <email>j@s.com</email>" + "  </Employee>"
            + "  <Employee emplid='2' type='admin'>" + "    <firstname>S</firstname>"
            + "    <lastname>H</lastname>" + "    <age>32</age>" + "    <email>s@h.com</email>"
            + "  </Employee>" + "</Employees>";
    List<String> ids = Arrays.asList("1", "2");
    for (int i = 0; i < ids.size(); i++) {
        String employeeId = ids.get(i);
        String xpath = "/Employees/Employee[@emplid='" + employeeId + "']/firstname";
        XPath xPath = XPathFactory.newInstance().newXPath();
        String employeeFirstName = xPath.evaluate(xpath, new InputSource(new StringReader(xml)));
        if (employeeFirstName == "") {
            System.out.println("Employee " + employeeId + " has no first name.");
        } else {//from   www .java  2s .c o  m
            System.out.println("Employee " + employeeId + "'s first name is " + employeeFirstName);
        }
    }
}

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())));

        findByID(doc, "uniqueID");

        System.out.println(documentToString(doc));

    }//  w w w .ja v a2  s . c om

From source file:MainClass.java

public static void main(String args[]) {
    try {/* www . j  a v a2  s .  c  o m*/
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler() {
            boolean name = false;

            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                if (qName.equalsIgnoreCase("NAME")) {
                    name = true;
                }
            }

            public void characters(char ch[], int start, int length) throws SAXException {
                if (name) {
                    System.out.println("Name: " + new String(ch, start, length));
                    name = false;
                }
            }
        };

        saxParser.parse(new InputSource(new StringReader(xmlString)), handler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "<p xmlns='http://www.java2s.com/nfe' versao='2.00'></p>";

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    NamespaceContext context = new NamespaceContext() {
        String PREFIX = "nfe";
        String URI = "http://www.java2s.com/nfe";

        @Override/* www . j  av  a2s  .  c o  m*/
        public String getNamespaceURI(String prefix) {
            return (PREFIX.equals(prefix)) ? URI : XMLConstants.NULL_NS_URI;
        }

        @Override
        public String getPrefix(String namespaceUri) {
            return (URI.equals(namespaceUri)) ? PREFIX : XMLConstants.DEFAULT_NS_PREFIX;
        }

        @Override
        public Iterator getPrefixes(String namespaceUri) {
            return Collections.singletonList(this.getPrefix(namespaceUri)).iterator();
        }
    };
    xPath.setNamespaceContext(context);
    InputSource inputSource = new InputSource(new StringReader(source));
    String versao = xPath.evaluate("//nfe:p/@versao", inputSource);
    System.out.println(versao.toString());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);// w ww  . ja  v  a2 s .  c  om
    o.setDescription("New order");
    o.setOrderDate(new Date());

    List<Item> items = new ArrayList<Item>();

    Item i = new Item();
    i.setName("PC");
    i.setQty(10);
    items.add(i);

    i = new Item();
    i.setName("Box");
    i.setQty(4);

    items.add(i);

    o.setItems(items);
    // Write it
    JAXBContext ctx = JAXBContext.newInstance(Order.class);

    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    m.marshal(o, sw);
    sw.close();
    System.out.println(sw.toString());

    // Read it back
    JAXBContext readCtx = JAXBContext.newInstance(Order.class);
    Unmarshaller um = readCtx.createUnmarshaller();

    Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString()));
    System.out.println(newOrder);
}