Traitement des attributs

Programme

// Analyse4.java

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class Analyse4 extends DefaultHandler {
  static private FileWriter dest;
  public static void main (String argv[])
  throws IOException {
    if (argv.length != 2) {
      System.err.println
         ("Usage: Analyse4 fichierSource fichierCible");
      System.exit (1);
    }
    File fichierDest = new File(argv[1]);
    dest = new FileWriter(fichierDest);
    DefaultHandler handler = new Analyse4();
    SAXParserFactory factory = S
              AXParserFactory.newInstance();
    try {
      SAXParser saxParser = factory.newSAXParser();
      saxParser.parse( new File(argv[0]), handler );
    } catch (Throwable t) {
      t.printStackTrace ();
      System.exit (2);
    }
    System.exit (0);
  }
  public void error(SAXParseException e)
  throws SAXParseException
  {
    throw e;
  }
  public void startDocument ()
  throws SAXException {
    ecrit("startDocument");
    nl();
  }
  public void endDocument ()
  throws SAXException {
    ecrit("endDocument");
    nl();
  }
  public void startElement (String namespaceURI,
                             String simpleName,
                             String qualifiedName,
                             Attributes attrs)
    throws SAXException {
    String  elementName = simpleName;
    if (elementName.equals(""))
      elementName = qualifiedName;
    ecrit("startElement : "+ elementName);
    // le nom de l'élément selon que le 
    // namespace est pris ou non en charge
    nl();
    if (attrs != null)
    // recuperation des attributs de l'élément
    // nombre : getLength,  valeur : getValue
    // nom : getLocalName ou getQName selon
    // namespace ou pas
      for (int i = 0; i < attrs.getLength(); i++) {
        ecrit("attributs :"); nl();
        String attName = attrs.getLocalName(i);
        if (attName.equals(""))
          attName = attrs.getQName(i);
        ecrit(attName+"=\""+attrs.getValue(i)+"\"");
        nl();
      }
  }
  public void endElement (String namespaceURI,
                           String simpleName,
                           String qualifiedName)
  throws SAXException {
    String  elementName = simpleName;
    if (elementName.equals(""))
      elementName = qualifiedName;
    ecrit("endElement : "+ elementName);
    nl();
  }
  public void characters (char buf [], int offset, int len)
  throws SAXException {
    String s = new String(buf, offset, len);
    ecrit (s);
    nl();
  }
  private void nl()
  throws SAXException {
    String lineEnd =  System.getProperty("line.separator");
    try {
      dest.write (lineEnd);
    } catch (IOException e) {
      throw new SAXException ("I/O error", e);
    }
  }
  private void ecrit(String s)
  throws SAXException {
    try {
      dest.write (s);
      dest.flush ();
    } catch (IOException e) {
      throw new SAXException ("I/O error", e);
    }
  }
}

commandes et résultats

java Analyse4 dialogue6.xml result.txt
[ferment@didier xml]$ more result.txt
startDocument
startElement : dialogue
startElement : situation
acte I, Scene 1 : madame pernelle et flipote sa servante,elmire, mariane, dorine, damis, cléante.
startElement : situation
startElement : replique
startElement : personnage
attributs :
attitude="pressée"
attributs :
geste="marchant vite"
madame pernelle
startElement : personnage
startElement : texte
attributs :
ton="fort"
Allons, Flipote, allons, que d'eux je me délivre.
endElement : texte
endElement : replique
startElement : replique
startElement : personnage
attributs :
attitude="essouflée"
elmire
endElement : personnage
startElement : texte
attributs :
ton="normal"
    l'attribut ne figure pas dans le document XML initial, mais il
     est défini par défaut avec la valeur 'normal'

Vous marchez d'un tel pas qu'on a peine à vous suivre.
endElement : texte
endElement : replique
startElement : replique
startElement : personnage
attributs :
attitude="agacée"
....

exercice :

Changer les traitements pour obtenir :
madame pernelle, pressée, marchant vite, sur un ton fort : "Allons, Flipote, allons, que d'eux je me délivre."
elmire, sur un ton normal : "Vous marchez d'un tel pas qu'on a peine à vous suivre."
...

 index  suivant