1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package com.testandroid;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParserXMLHandler extends DefaultHandler
{
// Nom des tags XML
private final String T_CLIENT = "t_client";
private final String IDCLI = "idCli";
private final String NOMCLI = "nomCli";
private final String PRENOMCLI = "prenomCli";
// Arraylist Client
private ArrayList<ListClient> listClient;
//Boolean permettant de savoir si à l'interieur d'un client
private boolean inClient;
// Client courante
private ListClient currentClient;
// Buffer contenant données d'un tag XML
private StringBuffer buffer;
@Override
public void processingInstruction(String target, String data) throws SAXException
{
super.processingInstruction(target, data);
}
public ParserXMLHandler()
{
super();
}
// évenement permettant d'initialiser tout avant le début du parcours du document XML
@Override
public void startDocument() throws SAXException
{
super.startDocument();
listClient = new ArrayList<ListClient>();
}
// Fonction étant déclenchée lorsque le parser trouve un tag XML
// C'est cette méthode que nous allons utiliser pour instancier un nouveau feed
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
{
// Réinitialise le buffer a chaque fois qu'il rencontre un item
buffer = new StringBuffer();
// localName contient le nom du tag rencontré
// Rencontre un tag T_CLIENT, Instanciation d'un nouveau client
if (localName.equalsIgnoreCase(T_CLIENT))
{
this.currentClient = new ListClient();
inClient = true;
}
// Définir des actions à effectuer pour chaque T_CLIENT rencontré (Si l'on veut !!!)
if (localName.equalsIgnoreCase(IDCLI))
{
// Nothing to do
}
if (localName.equalsIgnoreCase(NOMCLI))
{
// Nothing to do
}
if (localName.equalsIgnoreCase(PRENOMCLI))
{
// Nothing to do
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException
{
if (localName.equalsIgnoreCase(IDCLI))
{
if(inClient){
// Les caractères sont dans l'objet buffer
this.currentClient.setIdCli(buffer.capacity());
buffer = null;
}
}
if (localName.equalsIgnoreCase(NOMCLI))
{
if(inClient){
this.currentClient.setNomCli(buffer.toString());
buffer = null;
}
}
if (localName.equalsIgnoreCase(PRENOMCLI))
{
if(inClient){
this.currentClient.setPrenomCli(buffer.toString());
buffer = null;
}
}
if (localName.equalsIgnoreCase(T_CLIENT))
{
listClient.add(currentClient);
inClient = false;
}
}
public void characters(char[] ch,int start, int length) throws SAXException
{
String lecture = new String(ch,start,length);
if(buffer != null) buffer.append(lecture);
}
// cette méthode nous permettra de récupérer les données
public ArrayList<ListClient> getData()
{
return listClient;
}
} |
Partager