StAX Parser - Analizza documento XML

Esempio demo

Ecco il file xml di input che dobbiamo analizzare:

<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>
   </student>
   
   <student rollno = "493">
      <firstname>Vineet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

StAXParserDemo.java

package com.tutorialspoint.xml;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXParserDemo {
   public static void main(String[] args) {
      boolean bFirstName = false;
      boolean bLastName = false;
      boolean bNickName = false;
      boolean bMarks = false;
      try {
         XMLInputFactory factory = XMLInputFactory.newInstance();
         XMLEventReader eventReader =
         factory.createXMLEventReader(new FileReader("input.txt"));

         while(eventReader.hasNext()){
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()){
               case XMLStreamConstants.START_ELEMENT:
                  StartElement startElement = event.asStartElement();
                  String qName = startElement.getName().getLocalPart();
                  if (qName.equalsIgnoreCase("student")) {
                     System.out.println("Start Element : student");
                     Iterator<Attribute> attributes = startElement.getAttributes();
                     String rollNo = attributes.next().getValue();
                     System.out.println("Roll No : " + rollNo);
                  } else if (qName.equalsIgnoreCase("firstname")) {
                     bFirstName = true;
                  } else if (qName.equalsIgnoreCase("lastname")) {
                     bLastName = true;
                  } else if (qName.equalsIgnoreCase("nickname")) {
                     bNickName = true;
                  } else if (qName.equalsIgnoreCase("marks")) {
                     bMarks = true;
                  }				        
                  break;
               case XMLStreamConstants.CHARACTERS:
                  Characters characters = event.asCharacters();
                  if(bFirstName){
                     System.out.println("First Name: " + characters.getData());
                     bFirstName = false;
                  }
                  if(bLastName){
                     System.out.println("Last Name: " + characters.getData());
                     bLastName = false;
                  }
                  if(bNickName){
                     System.out.println("Nick Name: " + characters.getData());
                     bNickName = false;
                  }
                  if(bMarks){
                     System.out.println("Marks: " + characters.getData());
                     bMarks = false;
                  }
                  break;
               case XMLStreamConstants.END_ELEMENT:
                  EndElement endElement = event.asEndElement();
                  if(endElement.getName().getLocalPart().equalsIgnoreCase("student")){
                     System.out.println("End Element : student");
                     System.out.println();
                  }
                  break;
            }		    
         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (XMLStreamException e) {
         e.printStackTrace();
      }
   }
}

Il programma sopra genererà il seguente risultato:

Start Element : student
Roll No : 393
First Name: Dinkar
Last Name: Kad
Nick Name: Dinkar
Marks: 85
End Element : student

Start Element : student
Roll No : 493
First Name: Vineet
Last Name: Gupta
Nick Name: Vinni
Marks: 95
End Element : student

Start Element : student
Roll No : 593
First Name: Jasvir
Last Name: Singh
Nick Name: Jazz
Marks: 90
End Element : student