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.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

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

        System.out.println(reader.getVersion());
        System.out.println(reader.isStandalone());
        System.out.println(reader.standaloneSet());
        System.out.println(reader.getEncoding());
        System.out.println(reader.getCharacterEncodingScheme());

        parseRestOfDocument(reader);
    }

    private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

        while (reader.hasNext()) {
            int type = reader.next();
            switch (type) {
            case XMLStreamConstants.START_ELEMENT:
                System.out.println(reader.getLocalName());
                if (reader.getNamespaceURI() != null) {
                    String prefix = reader.getPrefix();
                    if (prefix == null) {
                        prefix = "[None]";
                    }
                    System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                }

                if (reader.getAttributeCount() > 0) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                + reader.getAttributeValue(i) + "')");
                        String attURI = reader.getAttributeNamespace(i);
                        if (attURI != null) {
                            String attPrefix = reader.getAttributePrefix(i);
                            if (attPrefix == null || attPrefix.equals("")) {
                                attPrefix = "[None]";
                            }
                            System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                        }
                    }
                }

                break;
            case XMLStreamConstants.END_ELEMENT:
                System.out.println("XMLStreamConstants.END_ELEMENT");
                break;
            case XMLStreamConstants.CHARACTERS:
                if (!reader.isWhiteSpace()) {
                    System.out.println("CD:" + reader.getText());
                }
                break;
            case XMLStreamConstants.DTD:
                System.out.println("DTD:" + reader.getText());
                break;
            case XMLStreamConstants.SPACE:
                System.out.println(" ");
                break;
            case XMLStreamConstants.COMMENT:
                System.out.println(reader.getText());
                break;
            default:
                System.out.println(type);
            }
        }
    }
}