Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileInputStream;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class Main {
    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml"))));

        boolean inline = false;
        StringBuffer sb = new StringBuffer();
        while (xmlr.hasNext()) {
            XMLEvent event = xmlr.nextEvent();

            if (event.isStartElement()) {
                StartElement element = (StartElement) event;
                if ("data".equals(element.getName().toString().trim())) {
                    inline = true;
                }
            }

            if (inline) {
                sb.append(xmlr.peek());
            }

            if (event.isEndElement()) {
                EndElement element = (EndElement) event;
                if ("data".equals(element.getName().toString().trim())) {
                    inline = false;
                    System.out.println(sb.toString());
                    sb.setLength(0);
                }
            }
        }
    }
}