Modélisation et interopérabilité : Semaine 49, cours 11

Documents pareils
Programmation des Applications Réparties. Parsers XML DOM et SAX

Master d Informatique Corrigé du partiel novembre 2010

Introduction à Java. Matthieu Herrb CNRS-LAAS. Mars

XML et Java Vue Balisée en XML. Programmation avancée NFP 121. XML pourquoi faire? Structuration des données. Titre Auteur Section Paragraphe

Serveur d'archivage 2007 Installation et utilisation de la BD exist

Projet de programmation (IK3) : TP n 1 Correction

Corrigé des exercices sur les références

Structure d un programme et Compilation Notions de classe et d objet Syntaxe

Cette application développée en C# va récupérer un certain nombre d informations en ligne fournies par la ville de Paris :

Programme Compte bancaire (code)

Corrigés des premiers exercices sur les classes

Une introduction à Java

TD3: tableaux avancées, première classe et chaînes

Programmation Réseau. Sécurité Java. UFR Informatique jeudi 4 avril 13

as Architecture des Systèmes d Information

OpenPaaS Le réseau social d'entreprise

Paris Airports - Web API Airports Path finding

Classe ClInfoCGI. Fonctions membres principales. Gestion des erreurs

Exceptions. 1 Entrées/sorties. Objectif. Manipuler les exceptions ;

Cours intensif Java. 1er cours: de C à Java. Enrica DUCHI LIAFA, Paris 7. Septembre Enrica.Duchi@liafa.jussieu.fr

Licence Bio Informatique Année Premiers pas. Exercice 1 Hello World parce qu il faut bien commencer par quelque chose...

Java Licence Professionnelle CISII,

TP, première séquence d exercices.

Langage Java. Classe de première SI

PROGRAMMATION PAR OBJETS

La base de données XML exist. A. Belaïd

TP n 2 Concepts de la programmation Objets Master 1 mention IL, semestre 2 Le type Abstrait Pile

Java DataBaseConnectivity

Faculté de Génie Chaire industrielle en infrastructures de communication. La technologie XML. Wajdi Elleuch

Utilisation d objets : String et ArrayList

La persistance des données dans les applications : DAO, JPA, Hibernate... COMPIL 2010 francois.jannin@inp-toulouse.fr 1

TD2 Programmation concurrentielle

Calcul Parallèle. Cours 5 - JAVA RMI

Premiers Pas en Programmation Objet : les Classes et les Objets

Chapitre 10. Les interfaces Comparable et Comparator 1

Plan du cours. Historique du langage Nouveautés de Java 7

Java Licence Professionnelle CISII,

Généralités sur le Langage Java et éléments syntaxiques.

Synchro et Threads Java TM

Encapsulation. L'encapsulation consiste à rendre les membres d'un objet plus ou moins visibles pour les autres objets.

Remote Method Invocation Les classes implémentant Serializable

Threads. Threads. USTL routier 1

INTRODUCTION A JAVA. Fichier en langage machine Exécutable

ACTIVITÉ DE PROGRAMMATION

Le prototype de la fonction main()

Exercices sur les interfaces

Cours 14 Les fichiers

Le hub d entreprise est une application de déploiement des applications mais aussi un outil de communication

Cours 1: Java et les objets

Package Java.util Classe générique

Tp 1 correction. Structures de données (IF2)

LMI 2. Programmation Orientée Objet POO - Cours 9. Said Jabbour. jabbour@cril.univ-artois.fr

Applet pour visualiser les variables «automate» notifiées

Programmation Orientée Objet Java

Auto-évaluation Programmation en Java

Programmer en JAVA. par Tama

TP1 : Initiation à Java et Eclipse

Introduction : les processus. Introduction : les threads. Plan

Programmation par les Objets en Java

RMI le langage Java XII-1 JMF

Page 1 sur 5 TP3. Thèmes du TP : l la classe Object. l Vector<T> l tutorial Interfaces. l Stack<T>

TD Objets distribués n 3 : Windows XP et Visual Studio.NET. Introduction à.net Remoting

Cours 1 : Introduction. Langages objets. but du module. contrôle des connaissances. Pourquoi Java? présentation du module. Présentation de Java

Chapitre V. Les classes : Object, Vector, etc.

34. DOM (Document Object Model)

Java Licence Professionnelle CISII, Cours 2 : Classes et Objets

27/11/12 Nature. SDK Python et Java pour le développement de services ACCORD Module(s)

Utiliser Java sans BlueJ

Lambda! Rémi Forax Univ Paris-Est Marne-la-Vallée

BASE DE DONNÉES XML NATIVE

INITIATION AU LANGAGE JAVA

Travaux Pratiques : Lucène - Gestion d un index plein texte

Le Modèle de Sécurité dans JAVA

Interfaces graphiques avec l API Swing

Programmation avec des objets : Cours 7. Menu du jour

Construire une base de donnée pour bibliothèque

GUI Client in HomeSIP Project

SYNC FRAMEWORK AVEC SQLITE POUR APPLICATIONS WINDOWS STORE (WINRT) ET WINDOWS PHONE 8

Java Licence Professionnelle Cours 7 : Classes et méthodes abstraites

Développement Logiciel

Remote Method Invocation (RMI)

RAPPELS SUR LES METHODES HERITEES DE LA CLASSE RACINE Object ET LEUR SPECIALISATION (i.e. REDEFINITION)

Introduction à JDBC. Accès aux bases de données en Java

Info0101 Intro. à l'algorithmique et à la programmation. Cours 3. Le langage Java

Alfstore workflow framework Spécification technique

Flux de données Lecture/Ecriture Fichiers

Introduction au langage Java

Derrière toi Une machine virtuelle!

Desktop Intégration. Rémi Forax

IFT287 Exploitation de base de données relationnelles et orientées objet. Laboratoire Mon premier programme Java en Eclipse

Design patterns. Design patterns - définition. Design patterns - avantages

Programmation Orientée Objet - Licence TIS CM8/9. Rappel sur la séance précédente. Lancelot Pecquet Lancelot.Pecquet@math.univ-poitiers.

Convers3 Documentation version Par Eric DAVID : vtopo@free.fr

Environnements de développement (intégrés)

Programmation Réseau SSH et TLS (aka SSL)

COMPARAISONDESLANGAGESC, C++, JAVA ET

Programmation en Java IUT GEII (MC-II1) 1

Notes de cours Practical BigData

J2SE Threads, 1ère partie Principe Cycle de vie Création Synchronisation

OWASP Code Review Guide Revue de code. Paris The OWASP Foundation. Victor Vuillard

Transcription:

Modélisation et interopérabilité : Semaine 49, cours 11 Benoît Valiron <benoit.valiron@monoidal.net> http://inf356.monoidal.net/ 1

Simple API for XML (SAX) 2

Lecture d'un document XML On a vu : DOM Document : Structure d'arbre Problème : tout le document est chargé en mémoire Avantage : accès aléatoire simple Aujourd'hui : traitement évènementiel. Document : lu par un automate Génération d'un flux d'évènements 3

Évènements? Rapport des choses rencontrées au cours de la lecture. Chaque événement peut donner certaines infos. startdocument enddocument startelement : String namespaceuri, localname, qualifiedname; Attributes atts; endelement String namespaceuri, localname, qualifiedname; characters et ignorablewhitespaces char[] text; int start, length; (5 autres évènements) 4

Exemple 5

Exemple Évenement : startdocument Attributs : (aucun) 6

Exemple Évenement : startelement Attributs : namespaceuri : '' localname : livre qualifiedname: livre atts : {'lang' 'fr'} 7

Exemple Évenement : ignorablewhitespace Attributs : text : '\n ' start : 0 length : 3 8

Exemple Évenement : startelement Attributs : namespaceuri : '' localname : titre qualifiedname: titre atts : {} 9

Exemple Évenement : characters Attributs : text : '' start : 7 length : 3 Note : le texte peut être n'importe quel sousensemble, pour autant que start et length correspondent 10

Exemple Évenement : endelement Attributs : namespaceuri : '' localname : titre qualifiedname: titre 11

Exemple Évenement : ignorablewhitespace Attributs : text : '>\n <' start : 1 length : 3 12

Exemple Évenement : startelement Attributs : namespaceuri : '' localname : auteur qualifiedname: auteur atts : {} 13

Exemple Évenement : characters Attributs : text : 'Chagnon</au' start : 0 length : 7 14

Exemple Évenement : endelement Attributs : namespaceuri : '' localname : auteur qualifiedname: auteur 15

Exemple Évenement : ignorablewhitespace Attributs : text : '\n <' start : 0 length : 3 16

Exemple Évenement : endelement Attributs : namespaceuri : '' localname : livre qualifiedname: livre 17

Exemple Évenement : enddocument Attributs : (aucun) 18

Exemple Résumé : startdocument startelement ignorablewhitespace startelement characters endelement ignorablewhitespace startelement characters endelement ignorablewhitespace endelement enddocument 19

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> 20

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : startdocument Attributs : (aucun) 21

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : startelement Attributs : namespaceuri : 'http://ma-biographie' localname : 'bio' qualifiedname: 'bio' atts : {'lang' 'fr'} 22

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : ignorablewhitespace Attributs : text : '\n <' start : 0 length : 3 23

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : startelement Attributs : namespaceuri : 'http://ma-biographie' localname : 'nom' qualifiedname: 'nom' atts : {'né' '1912' ; 'mort' '1954' } 24

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : characters Attributs : text : '>Turing</nom' start : 1 length : 6 25

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : endelement Attributs : namespaceuri : 'http://ma-biographie' localname : 'nom' qualifiedname: 'nom' 26

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : characters Attributs : text : 'nom>\n est un <' start : 4 length : 10 27

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : startelement Attributs : namespaceuri : 'http://www.w3.org/1999/xhtml' localname : 'i' qualifiedname: 'h:i' atts : {} 28

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : characters Attributs : text : '<h:i>informaticien' start : 5 length : 13 29

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : endelement Attributs : namespaceuri : 'http://www.w3.org/1999/xhtml' localname : 'i' qualifiedname: 'h:i' 30

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : characters Attributs : text : '.\n' start : 0 length : 2 31

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : endelement Attributs : namespaceuri : 'http://ma-biographie' localname : 'bio' qualifiedname: 'bio' 32

Exemple avec espace de nom <bio lang= fr xmlns= http://ma-biographie xmlns:h= http://www.w3.org/1999/xhtml > <nom né= 1912 mort= 1954 >Turing</nom> est un <h:i>informaticien</h:i>. </bio> Évenement : enddocument Attributs : (aucun) 33

Push (SAX) versus Pull (StAX) Serveur : Génère des évènements test.xml : Client : Traite les évènements 34

Push test.xml : Serveur Lit test.xml Client 35

Push test.xml : Serveur Lit test.xml startdocument startelement ignorablewhitespace startelement... Le flux de données est géré par le client Client 36

Pull test.xml : ReadStartElement( titre ) Serveur Client 37

Pull test.xml : ReadStartElement( titre ) Serveur { localname = titre ; } Client 38

Pull test.xml : ReadStartElement( titre ) Serveur { localname = titre ; } readstring() Client 39

Pull test.xml : ReadStartElement( titre ) Serveur { localname = titre ; } readstring() Client XML Le flux de données est gérée par le serveur 40

Modèle Push (SAX) 41

SAX Utilisation d'un flot d'évènements On peut reconstruire un arbre... Mais peu utile! On ne veut justement plus d'arbre. Avec un automate : Un ensemble de variables Appelé environnement Initialisation lors de startdocument Modification à chaque événement rencontré Résultat lors de enddocument 42

Exemple : Nombre d'éléments Environnement: int number; Événements : startdocument : number = 0; enddocument : affiche number; startelement : ++number; Tous les autres : rien. 43

Exemple Environnement : int number; 44

Exemple Évenement : startdocument number = 0; Environnement : int number = 0; 45

Exemple Évenement : startelement ++number; Environnement : int number = 1; 46

Exemple Évenement : ignorablewhitespace (rien) Environnement : int number = 1; 47

Exemple Évenement : startelement ++number; Environnement : int number = 2; 48

Exemple Évenement : characters (rien) Environnement : int number = 2; 49

Exemple Évenement : endelement (rien) Environnement : int number = 2; 50

Exemple Évenement : ignorablewhitespace (rien) Environnement : int number = 2; 51

Exemple Évenement : startelement ++number; Environnement : int number = 3; 52

Exemple Évenement : characters (rien) Environnement : int number = 3; 53

Exemple Évenement : endelement (rien) Environnement : int number = 3; 54

Exemple Évenement : ignorablewhitespace (rien) Environnement : int number = 3; 55

Exemple Évenement : endelement (rien) Environnement : int number = 3; 56

Exemple Évenement : enddocument Sur la sortie standard : 3 Affiche number Environnement : int number = 3; 57

package org.xml.sax; En java? public interface XMLReader { public void setcontenthandler(contenthandler handler); public void parse (String filename); } Public final static class XMLReaderFactory { public static XMLReader createxmlreader(); public static XMLReader createxmlreader(string classname); } public interface ContentHandler { public void startdocument(); public void enddocument(); public void startelement(string nsuri, String localname, String qname, Attributes atts); public void endelement(string nsuri, String localname, String qname); public void characters(char[] text, int start, int length); public void ignorablewhitespace(char[] text, int start, int length); } public void setdocumentlocator(locator l); public void startprefixmapping(string prefix, String uri); public void endprefixmapping(string prefix); public void processinginstruction(string target, String data); public void skippedentity(string name); 58

En java? XMLReaderFactory : pour créer un automate vide ContentHandler : pour décrire un automate Méthode setcontenthandler de XMLReader : associe une description de l'automate à un automate Méthode parse de XMLReader : lance l'automate sur un document XML. 59

Exemple : Nombre d'éléments import org.xml.sax.*; public class CompteElement implements ContentHandler { int number; public void startdocument() { number = 0; } public void enddocument() { System.out.println(number) } public void startelement(string n, String l, String q, Attributes a) { ++number; } public void endelement(string n, String l, String q) {} public void characters(char[] text, int start, int length) {} public void ignorablewhitespace(char[] text, int start, int length) {} } public void setdocumentlocator(locator l) {} public void startprefixmapping(string prefix, String uri) {} public void endprefixmapping(string prefix) {} public void processinginstruction(string target, String data) {} public void skippedentity(string name) {} 60

Exemple : Nombre d'éléments des imports public class Main { public static void main(string[] argv) { XMLReader parser; String filename = test.xml ; parser = XMLReaderFactory.createXMLReader(); parser.setcontenthandler(new CompteElement()); parser.parse(filename); } } 61

Exercice 1 : Que cela fait-il? import org.xml.sax.*; public class Exo1 implements ContentHandler { int number1; int number2; } public void startdocument() { number1 = 0; number2 = 0; } public void enddocument() { System.out.println(number2); } public void startelement(string n, String l, String q, Attributes a) { ++number1; if ( number1 > number2 ) { number2 = number1; } } public void endelement(string n, String l, String q) { --number1; } public void characters(char[] text, int start, int length) {} public void ignorablewhitespace(char[] text, int start, int length) {} public void setdocumentlocator(locator l) {} public void startprefixmapping(string prefix, String uri) {} public void endprefixmapping(string prefix, String uri) {} public void processinginstruction(string target, String data) {} public void skippedentity(string name) {} 62

Exercice 2 : Que cela fait-il? import org.xml.sax.*; public class Exo2 implements ContentHandler { boolean t; } public void startdocument() { t = false; } public void enddocument() {} public void startelement(string n, String l, String q, Attributes a) { if (l.equals("titre")) t = true; } public void endelement(string n, String l, String q) { t = false; } public void characters(char[] text, int start, int length) { if (t) { String s = new String(text); System.out.println(s.substring(start,start+length)); } } public void ignorablewhitespace(char[] text, int start, int length) {} public void setdocumentlocator(locator l) {} public void startprefixmapping(string prefix, String uri) {} public void endprefixmapping(string prefix, String uri) {} public void processinginstruction(string target, String data) {} public void skippedentity(string name) {} 63

Exercice 3 : Que cela fait-il? public class Exo3 implements ContentHandler { Document doc; boolean t; Element root; } public void startdocument() { DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = dbfac.newdocumentbuilder(); doc = docbuilder.newdocument(); root = doc.createelement( biblio ); } public void enddocument() { doc.appendchild(root); FONCTION_D_AFFICHAGE(doc); } public void startelement(string n, String l, String q, Attributes a) { if (l.equals("titre")) t = true; } public void endelement(string n, String l, String q) { t = false; } public void characters(char[] text, int start, int length) { if (t) { String s = new String(text); Element e = doc.createelement( livre ); e.setattribute( titre, s.substring(start,start+length)); root.appendchild(e); } } public void ignorablewhitespace(char[] text, int start, int length) {} public void setdocumentlocator(locator l) {} public void startprefixmapping(string prefix, String uri) {} public void endprefixmapping(string prefix, String uri) {} public void processinginstruction(string target, String data) {} public void skippedentity(string name) {} 64