Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

List of usage examples for javax.xml.stream XMLInputFactory createXMLStreamReader

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory createXMLStreamReader.

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("data.xml"));
    xsr.nextTag(); // advance to Employees tag
    xsr.nextTag(); // advance to first Employer element
    Map<String, String> map = new HashMap<String, String>();
    while (xsr.getLocalName().equals("Employee")) {
        map.put(xsr.getAttributeValue("", "id"), xsr.getElementText());
        xsr.nextTag(); // advance to next Employer element
    }/*from ww w . jav a2s .c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml");
        t.transform(new StAXSource(xsr), new StreamResult(file));
    }/*from  w  ww . jav  a 2 s.co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        File file = new File("out" + xsr.getAttributeValue(null, "account") + ".xml");
        t.transform(new StAXSource(xsr), new StreamResult(file));
    }//from   w  w  w . j a va2 s  .  co m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        DOMResult result = new DOMResult();
        t.transform(new StAXSource(xsr), result);
        Node domNode = result.getNode();
    }//from w  w  w  .j av a 2 s .c  om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
    System.out.println(sr.getClass());

    while (sr.hasNext()) {
        int eventType = sr.next();

        if (eventType == XMLStreamReader.START_DOCUMENT) {
            continue;
        } else if (eventType == XMLStreamReader.END_ELEMENT) {
            System.out.println("End Element:    " + sr.getLocalName());
        } else if (eventType == XMLStreamReader.START_ELEMENT) {
            System.out.println("Start Element:  " + sr.getLocalName());
        }//from  w w  w . j av a 2 s . c o m
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));

    int eventTypeID = reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, null, "person");

    eventTypeID = reader.nextTag();/*w  w  w . ja va2s  .  c o  m*/
    reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    System.out.println(reader.getElementText());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File file = new File("test.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));

    while (reader.getEventType() == 6)
        reader.next();//from w  w  w. j av a  2  s . c o  m

    int eventTypeID = reader.next();
    System.out.println("Hello " + reader.getText());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));

    int eventTypeID = reader.nextTag();

    eventTypeID = reader.nextTag();// w  ww  . j  ava  2 s  .c  o  m

    eventTypeID = reader.next();
    System.out.println("Hello " + reader.getText());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLStreamReader xmlsr = xmlif.createXMLStreamReader(new FileReader("points.xml"));
    int eventType;
    while (xmlsr.hasNext()) {
        eventType = xmlsr.next();/*from w w  w .  j av a2 s.c  om*/
        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            System.out.println(xmlsr.getName());
            break;
        case XMLEvent.CHARACTERS:
            System.out.println(xmlsr.getText());
            break;
        default:
            break;
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();

    StringWriter sw = new StringWriter();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(sw);
    xsw.writeStartDocument();// ww  w. j a va  2 s .  c o m
    xsw.writeStartElement("foo");
    xsw.writeCharacters("<>\"&'");
    xsw.writeEndDocument();

    String xml = sw.toString();
    System.out.println(xml);

    // READ THE XML
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
    xsr.nextTag(); // Advance to "foo" element
    System.out.println(xsr.getElementText());
}