Example usage for javax.xml.stream XMLStreamConstants START_ELEMENT

List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants START_ELEMENT.

Prototype

int START_ELEMENT

To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.

Click Source Link

Document

Indicates an event is a start element

Usage

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  ww . j ava2 s  .  com*/
    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 {
    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  ww  .ja  v a 2  s.  c  o m
    try {
        reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    } catch (XMLStreamException e) {
        System.out.println("Assertion failed. " + e.getMessage() + " at " + reader.getLocation().getLineNumber()
                + ":" + reader.getLocation().getColumnNumber());
    }
    System.out.println(reader.getElementText());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URLConnection connection = new URL("http://api.androidhive.info/pizza/?format=xml").openConnection();
    InputStream inputStream = connection.getInputStream();
    XMLStreamReader streamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream, "UTF-8");
    while (streamReader.hasNext())
        if (streamReader.next() == XMLStreamConstants.START_ELEMENT)
            System.out.println("START_ELEMENT " + streamReader.getName());
}

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));
    }/*www.  j ava 2  s  .com*/
}

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  w  w . j a va 2  s  .  c  o 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  ww w . j a v  a2 s  .c om*/
}

From source file:CursorApproachEventObject.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    xmlif.setEventAllocator(new XMLEventAllocatorImpl());
    allocator = xmlif.getEventAllocator();

    XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));

    int eventType = xmlr.getEventType();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();//  w  w w  .j a  va2 s .  com
        if ((eventType == XMLStreamConstants.START_ELEMENT) && xmlr.getLocalName().equals("Book")) {
            StartElement event = getXMLEvent(xmlr).asStartElement();
            System.out.println("EVENT: " + event.toString());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File source = new File(args[0]);
    File target = new File(source + ".new");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    InputStream in = new FileInputStream(source);
    XMLEventReader reader = inputFactory.createXMLEventReader(in);

    OutputStream out = new FileOutputStream(target);
    XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
    XMLEvent event;/*from w  w  w  . j a  v a 2  s .  c  o  m*/

    boolean deleteSection = false;
    while (reader.hasNext()) {
        event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) {
            deleteSection = true;
            continue;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) {
            deleteSection = false;
            continue;
        } else if (deleteSection) {
            continue;
        } else {
            writer.add(event);
        }
    }
}

From source file:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;/* w  w w  . j av  a2 s. c o m*/
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

From source file:Main.java

public static void skipElement(XMLStreamReader parser) throws XMLStreamException {
    int numEndTags = 1;
    while (numEndTags > 0) {
        switch (parser.next()) {
        case XMLStreamConstants.START_ELEMENT:
            ++numEndTags;//go down
            break;
        case XMLStreamConstants.END_ELEMENT:
            --numEndTags;//go up
            break;
        }/*  w  ww.jav  a  2s. c om*/
    }
}