Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

XML: Utilisation de ‘StreamFilter’ pour analyser le document


{filelink=982}

 
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.util.Date;
import javax.xml.namespace.QName;
 
public class AnalyseXML implements javax.xml.stream.StreamFilter
{
 
    public static final String getTypeEvt(int eventType) {
        switch (eventType)
        {
        case XMLEvent.START_ELEMENT:
	               return "DEBUT_ELEMENT";
 
	           case XMLEvent.START_DOCUMENT:
	               return "DEBUT_DOCUMENT";
 
	           case XMLEvent.END_ELEMENT:
	               return "FIN_ELEMENT";
 
	           case XMLEvent.END_DOCUMENT:
	               return "FIN_DOCUMENT";
 
	           case XMLEvent.CHARACTERS:
	               return "CHARACTERES";
 
	           case XMLEvent.ATTRIBUTE:
	               return "ATTRIBUT";
 
	          case XMLEvent.COMMENT:
	               return "COMMENTAIRE";
 
	           case XMLEvent.PROCESSING_INSTRUCTION:
	               return "INSTRUCTION_PROCESSUS";
 
	           case XMLEvent.ENTITY_REFERENCE:
	               return "REFERENCE_ENTITE";
 
	           case XMLEvent.DTD:
	               return "DTD";
 
	           case XMLEvent.CDATA:
	               return "CDATA";
 
        	case XMLEvent.SPACE:
                  return "CDATA";
        }
 
        return "UNKNOWN_EVENT_TYPE";
    }
 
    private static void affichageTypeEvt(int eventType) {
        System.out.print("Type Evénément: (" + eventType + "):");
        System.out.println(getTypeEvt(eventType));
    }
 
    private static void affichageNom
    (
        XMLStreamReader xmlr,
        int eventType)
        {
        if (xmlr.hasName())
        {
            System.out.println("Le Nom: " + xmlr.getLocalName());
        }
    }
 
    private static void affichageText(XMLStreamReader xmlr)
       {
        if (xmlr.hasText())
        {
            System.out.println("Le Texte: " + xmlr.getText());
        }
    }
 
    private static void affichageDonneePI(XMLStreamReader xmlr)
    {
        if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION)
        {
			System.out.println(" Donnée PI = " + xmlr.getPIData());
            System.out.println(" Cible  PI = " + xmlr.getPITarget());
 
        }
    }
 
    private static void affichageAttributs(XMLStreamReader xmlr)
    {
        if (xmlr.getAttributeCount() > 0)
        {
            System.out.println("Les Attributs: ");
 
            int compteur = xmlr.getAttributeCount();
 
            for (int i = 0; i < compteur; i++)
            {
                QName nom = xmlr.getAttributeName(i);
                String namespace = xmlr.getAttributeNamespace(i);
                String type = xmlr.getAttributeType(i);
                String prefix = xmlr.getAttributePrefix(i);
                String valeur = xmlr.getAttributeValue(i);
 
                System.out.println("PREFIX-ATTRIBUT: " + prefix);
                System.out.println("NOM-ATTRIBUT:   " + nom.toString());
                System.out.println("TYPE-ATTRIBUT:  " + type);
                System.out.println("NAMESP-ATTRIBUT: " + namespace);
                System.out.println("VALEUR-ATTRIBUT:  " + valeur);
 
            }
        }
    }
 
    public boolean accept(XMLStreamReader reader)
    {
        if (!reader.isStartElement() &#038;& !reader.isEndElement())
        {
            return false;
        } else
        {
            return true;
        }
    }
    public static void main(String[] args) throws Exception
    {
        String nomFichier = "test.xml";
 
        XMLInputFactory xmlFact = null;
 
        xmlFact = XMLInputFactory.newInstance();
        xmlFact.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);
        xmlFact.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
        xmlFact.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlFact.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
 
        System.out.println("Le FACTORY: " + xmlFact);
        System.out.println("nomFichier = " + nomFichier);
 
        FileInputStream fis = new FileInputStream(nomFichier);
 
        XMLStreamReader xmlr = xmlFact.createFilteredReader(xmlFact.createXMLStreamReader(fis),new AnalyseXML());
 
        int eventType = xmlr.getEventType();
        affichageTypeEvt(eventType);
 
        while (xmlr.hasNext())
        {
                eventType = xmlr.next();
                affichageTypeEvt(eventType);
                affichageNom(xmlr, eventType);
                affichageText(xmlr);
 
                if (xmlr.isStartElement())
                {
                    affichageAttributs(xmlr);
                }
                affichageDonneePI(xmlr);
        }
    }
 
}