IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Format d'échange (XML, JSON...) Java Discussion :

[Optimisation] Découper un fichier XML


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 135
    Points : 46
    Points
    46
    Par défaut [Optimisation] Découper un fichier XML
    Bonjour, j'ai un programme qui parse un fichier xml en java1.3 avec sax qui stoke toutes les lignes du documents dans un vecteur de ligne avec lequel je stocke ses lignes apres dans une fichier.
    Seulement en faisant cela, je me sui rendu compte que mon programme etait lent
    Par exemple si mon fichier XML ne fait que 530ko mon programme met 4minute pour s'exécuter

    J'aurais voulu savoir si qq1 connait des moyen pour réduire mon traitement et le rendre plus rapide

    Merci

    [ Modéré par Viena ]
    Ajout d'un tag et modification du titre : Pour la compréhension de tous, utilisez un titre clair et les tags ! merci.

    Les Règles du Forum

  2. #2
    Rédacteur
    Avatar de romaintaz
    Homme Profil pro
    Java craftsman
    Inscrit en
    Juillet 2005
    Messages
    3 790
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Java craftsman
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2005
    Messages : 3 790
    Points : 7 275
    Points
    7 275
    Par défaut Re: Optimisation XML et java
    Citation Envoyé par ujoodha
    j'ai un programme qui parse un fichier xml en java1.3 avec sax qui stoke toutes les lignes du documents dans un vecteur de ligne avec lequel je stocke ses lignes apres dans une fichier.
    A vrai dire, je n'ai pas compris grand chose là... Pourquoi tu stockes les lignes de ton fichier XML dans un vecteur de lignes ?
    Que veux tu faire des informations de ton fichier XML ?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 135
    Points : 46
    Points
    46
    Par défaut Réponse a ta question
    J'ai un doc XML qui contient par exemple une balise toto. cettebalise toto apparait plusieurs fois
    mon but est de créer un prg qui puissent générer plusieurs fisieurs xml avec une seule balise toto
    si g 5 balise toto alors j'aurai 5 fichiers dans lequel il y aura i seule fois la balise toto.

    Voila

  4. #4
    NGY
    NGY est déconnecté
    Membre habitué
    Inscrit en
    Août 2002
    Messages
    137
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 137
    Points : 164
    Points
    164
    Par défaut
    : J'ai rien compris.
    Mais de toutes façons, 4 min. pour parser un fichier de 530 ko, c'est trop !
    Montre-nous ton code.

    Sinon, quel est ta configuration matérielle (processeur et ram) ?

  5. #5
    Membre averti
    Inscrit en
    Avril 2004
    Messages
    503
    Détails du profil
    Informations forums :
    Inscription : Avril 2004
    Messages : 503
    Points : 445
    Points
    445
    Par défaut
    C'est clair que 4 min c'est trop !

    J'ai fait une appli il y a 2 ans qui parsait plusieurs fichiers dont 1 ou 2 faisaient 12Mo...en 1 min 30 sec.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 135
    Points : 46
    Points
    46
    Par défaut réponse a ta remarque
    ca marche avec 12 Mo tu peux me dire commetn tu as fait pour optimiser car a 4Mo j'ai une erreur du type outofmemory

    ca m'intéresserait de savoir comment tu as fait ?

    Merci

  7. #7
    Membre averti
    Inscrit en
    Août 2005
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 352
    Points : 427
    Points
    427
    Par défaut
    Je ne sais pas ce que Regis.C a pu faire mais ca a sans doute à voir avec la différence entre les parseurs SAX (évenementiel) et DOM (modèle objet d'un document).
    Je me trompe ?

  8. #8
    Membre expérimenté
    Avatar de viena
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    1 071
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Juillet 2002
    Messages : 1 071
    Points : 1 512
    Points
    1 512
    Par défaut
    ca a sans doute à voir avec la différence entre les parseurs SAX (évenementiel) et DOM (modèle objet d'un document).
    Il utilise Sax, je ne pense pas qu'une bonne façon d'optimiser serait de prendre Dom qui est beaucoup plus lourd !!

    As tu essayer de commencer par parser tes fichiers et creer uniquement des flux et creer les fichiers physiquement seulement après ?

  9. #9
    Membre averti
    Inscrit en
    Août 2005
    Messages
    352
    Détails du profil
    Informations forums :
    Inscription : Août 2005
    Messages : 352
    Points : 427
    Points
    427
    Par défaut
    Autant pour moi, j'avais pas vu.

    Ps : je ne suggérais pas d'utiliser DOM

  10. #10
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    Le probleme vient du code que tu executes dans ton handler SAX. Pourrais-tu nous le montrer ?

  11. #11
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 251
    Points
    1 251
    Par défaut Re: réponse a ta remarque
    Citation Envoyé par ujoodha
    a 4Mo j'ai une erreur du type outofmemory
    Si tu stockes chacune de tes lignes dans un vecteur, j'ai bien peur que ton vecteur ne fasse déjà plusieurs dizaines de milliers de lignes...
    Le problème avec les vecteurs, c'est qu'à chaque fois que leur size arrive à la limite de capacité, il y a une réallocation. Alors dans on cas, on peut imaginer un paquet de réallocation, gourmandes en temps et ressources.
    Une première solution pour éviter ces pertes de temps et d'espace consisterai à adapter le capacityIncrement de ton Vector. Pour un fichier de 4Mo, je n'hésiterai pas trop à le mettre à 10, 20 ou même 30'000. Comme ça, tu ne devrais pas avoir plus de 10 réallocations... Bien sûr j'adapterai volontiers une initialeSize à une valeur identique.

    Ensuite, je sais que ça peut paraître c**, mais une des nouveutés qui vient avec les nouvelles version de Java, et sa capacité à être plus rapide (JVM plus performante...) alors si tu peux changer ta version de Java, tu prend le risque de gagner du temps.

    Enfin, si tu pouvais nous montrer le code... Si ça se trouve, le temps excessif ne vient pas de la lecture, mais plutôt de l'écriture dans les autres fichiers, ou du traitement que tu fais entre temps.

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 135
    Points : 46
    Points
    46
    Par défaut Réponse a ta remarque
    je ne peux pas changer de jvm car je suis chez le client et tous les projets tourne en java1.3 c ca qui me fait chier

    Mais bon je vais essayer ton idéee pour la taille du veteur
    Merci

  13. #13
    NGY
    NGY est déconnecté
    Membre habitué
    Inscrit en
    Août 2002
    Messages
    137
    Détails du profil
    Informations forums :
    Inscription : Août 2002
    Messages : 137
    Points : 164
    Points
    164
    Par défaut
    Au risque de me répéter : On pourrait t'aider plus que ça si tu ne nous montrais pas ton code.
    Il y a plein de causes possibles à ton problème et il n'y a, à priori, pas de devins sur le forum !

  14. #14
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 135
    Points : 46
    Points
    46
    Par défaut Voila le code
    Code de mon Handler
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
     
    package fr.deskom.Outils;
     
    import java.util.ArrayList;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public class Handler extends DefaultHandler {
     
    	private boolean first = true;
     
    	private ArrayList listLine = new ArrayList();
     
    	private String strLineFac = "";
     
    	private String strLineTitu = "";
     
    	private String strLineGest = "";
     
    	private boolean root = true;
     
    	private BeanSFR sfrBean = null;
     
    	private boolean titulaire = false;
     
    	private boolean gestionnaire = false;
     
    	private boolean facture = false;
     
    	private boolean fadet = false;
     
    	public Handler() {
    		sfrBean = new BeanSFR();
    	}
     
    	public void characters(char[] ch, int start, int length)
    			throws SAXException {
    		// System.out.println(new String(ch).trim());
    		// String str = new String(new String(ch));
    		// str = str;
    		if (!fadet) {
    			if (first) {
    				String str = new String(new String(ch));
    				str = str.trim();
    				if (str.startsWith("<!DOCTYPE")) {
    					// System.out.println("dans le start " +
    					// str.substring(0,str.indexOf(">")));
    					sfrBean.setDtd(str.substring(0, str.indexOf(">")) + ">");
    				}
    				first = false;
    			}
     
    			if (titulaire) {
    				// System.out.println(new String(ch, start, length));
    				// if(new String(ch, start, length).indexOf("\n\r")!=-1)
    				// System.out.println("titulaire " + new String(ch).trim());
    				strLineTitu += new String(ch, start, length);
    				// sfrBean.addTitu(test(new String(ch, start, length)));
    				// titulaire = false;
    			} else if (gestionnaire) {
    				// System.out.println("gestionnaire " +new String(ch).trim());
    				// gestionnaire = false;
    				// if(!new String(ch, start, length).endsWith("\n"))
    				// sfrBean.addGest(test(new String(ch, start, length)));
    				strLineGest += new String(ch, start, length);
    			} else if (facture) {
    				strLineFac += new String(ch, start, length);
    				// listLine.add(test(new String(ch, start, length)));
    				// if(!new String(ch, start, length).endsWith("\n"))
    				// System.out.println("dans facture "+test(new String(ch, start,
    				// length)));
    				// sfrBean.addFactureSFRLine(test(new String(ch, start,
    				// length)));
    			}
    		}
    	}
     
    	public void endDocument() throws SAXException {
    		// super.endDocument();
    	}
     
    	public void endElement(String uri, String localName, String qName)
    			throws SAXException {
    		// super.endElement(uri, localName, qName);
    		// System.out.print("</" + qName + ">");
    		if (qName.equals("TITUL")) {
    			// sfrBean.addTitu("</" + qName + ">");
    			strLineTitu += "</" + qName + ">";
    			sfrBean.addTitu(strLineTitu);
    			strLineTitu = "";
    			titulaire = false;
    		} else if (qName.equals("GEST")) {
    			// System.out.println("gest");
    			strLineGest += "</" + qName + ">";
    			// sfrBean.addGest("</" + qName + ">");
    			sfrBean.addGest(strLineGest);
    			strLineGest = "";
    			gestionnaire = false;
    		} else if (qName.equals("FACTURESFR")) {
    			strLineFac += "</" + qName + ">";
    			// listLine.add("</" + qName + ">");
    			// listLine.add(strLineFac);
    			facture = false;
    			sfrBean.addFacture(strLineFac);
    			strLineFac = "";
    //			listLine = null;
    //			listLine = new ArrayList();
    		} else if (qName.endsWith("FADET")) {
    			facture = true;
    			fadet = false;
    		}
    		if (titulaire) {
    			strLineTitu += "</" + qName + ">";
    			// sfrBean.addTitu("</" + qName + ">");
    		} else if (gestionnaire) {
    			strLineGest += "</" + qName + ">";
    			// sfrBean.addGest("</" + qName + ">");
    		} else if (facture) {
    			strLineFac += "</" + qName + ">";
    			// listLine.add("</" + qName + ">");
    			// sfrBean.addFactureSFRLine("</" + qName + ">");
    			// sfrBean.addFacture(sfrBean.getFactureSFRLine());
    			// sfrBean.getFactureSFRLine().clear();
    		}
    	}
     
    	// public void endPrefixMapping(String prefix) throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.endPrefixMapping(prefix);
    	// }
    	//
    	// public void error(SAXParseException e) throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.error(e);
    	// }
    	//
    	// public void fatalError(SAXParseException e) throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.fatalError(e);
    	// }
    	//
    	// public void ignorableWhitespace(char[] ch, int start, int length)
    	// throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.ignorableWhitespace(ch, start, length);
    	// }
    	//
    	// public void notationDecl(String name, String publicId, String systemId)
    	// throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.notationDecl(name, publicId, systemId);
    	// }
    	//
    	// public void processingInstruction(String target, String data)
    	// throws SAXException {
    	// // TODO Auto-generated method stub
    	// // System.out.println(target+ " , " + data ) ;
    	// super.processingInstruction(target, data);
    	// }
    	//
    	// public InputSource resolveEntity(String publicId, String systemId)
    	// throws IOException, SAXException {
    	// // TODO Auto-generated method stub
    	// //System.out.println(publicId + " " + systemId);
    	// // System.out.println(super.resolveEntity(publicId,
    	// // systemId).getEncoding());
    	//
    	// // System.out.println(super.resolveEntity(publicId,
    	// // systemId).getPublicId());
    	// // System.out.println(super.resolveEntity(publicId,
    	// // systemId).getSystemId());
    	// return super.resolveEntity(publicId, systemId);
    	//
    	// }
     
    	// public void setDocumentLocator(Locator locator) {
    	// // TODO Auto-generated method stub
    	//
    	// super.setDocumentLocator(locator);
    	// }
    	//
    	// public void skippedEntity(String name) throws SAXException {
    	// // TODO Auto-generated method stub
    	// // System.out.println("skipp " +name);
    	// super.skippedEntity(name);
    	// }
     
    	public void startDocument() throws SAXException {
    		// TODO Auto-generated method stub
    		super.startDocument();
    	}
     
    	public void startElement(String uri, String localName, String qName,
    			Attributes attributes) throws SAXException {
    		// TODO Auto-generated method stub
    		// super.startElement(uri, localName, qName, attributes);
    		if (root) {
    			sfrBean.setRoot(qName);
    			root = false;
    		}
    		String line = getBalise(qName, attributes);
     
    		if (titulaire) {
    			// line = getBalise(qName, attributes);
    			strLineTitu += line;
    			// sfrBean.addTitu(strLineTitu);
    			// sfrBean.addTitu(line);
    		} else if (gestionnaire) {
    			// line = getBalise(qName, attributes);
    			strLineGest += line;
    			// sfrBean.addGest(line);
    		} else if (facture) {
    			// line = getBalise(qName, attributes);
    			strLineFac += line;
    			// listLine.add(line);
    		}
     
    		if (qName.equals("TITUL")) {
    			// System.out.println("titulaire");
    			titulaire = true;
    			strLineTitu += line;
    			// sfrBean.addTitu(strLineTitu);
    		} else if (qName.equals("GEST")) {
    			// System.out.println("gest");
    			gestionnaire = true;
    			strLineGest += line;
    			// sfrBean.addGest(strLineTitu);
    		} else if (qName.equals("FACTURESFR")) {
    			facture = true;
    			// listLine = new ArrayList();
    			strLineFac += line;
    			// listLine.add(line);
    		} else if (qName.endsWith("FADET")) {
    			facture = false;
    			fadet = true;
    		}
    	}
     
    	// public void startPrefixMapping(String prefix, String uri)
    	// throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.startPrefixMapping(prefix, uri);
    	// }
    	//
    	// public void unparsedEntityDecl(String name, String publicId,
    	// String systemId, String notationName) throws SAXException {
    	// // TODO Auto-generated method stub
    	// // System.out.println(name +" " +publicId+" "+ systemId+" , " +
    	// // notationName);
    	// super.unparsedEntityDecl(name, publicId, systemId, notationName);
    	// }
    	//
    	// public void warning(SAXParseException e) throws SAXException {
    	// // TODO Auto-generated method stub
    	// super.warning(e);
    	// }
     
    	public String getBalise(String qName, Attributes attributes)
    			throws SAXException {
    		String line = " ";
    		String attribut = " ";
    		line = "<" + qName + " ";
    		if (attributes != null) {
     
    			for (int i = 0; i < attributes.getLength(); i++) {
    				// try {
    				// System.out.println("value attribue "
    				// + new String(attributes.getValue(i).getBytes(
    				// "US-ASCII"), "US-ASCII"));
    				// System.out.println("value attribue "
    				// + new String(attributes.getValue(i).getBytes(
    				// "UTF-8"), "UTF-8"));
    				// System.out.println("value attribue "
    				// + new String(attributes.getValue(i).getBytes(
    				// "UTF-16"), "UTF-16"));
    				// System.out.println("value attribue "
    				// + new String(attributes.getValue(i).getBytes(
    				// "ISO-8859-1"), "ISO-8859-1"));
    				// } catch (UnsupportedEncodingException e) {
    				// // TODO Auto-generated catch block
    				// e.printStackTrace();
    				// }
    				attribut += attributes.getQName(i) + "=\""
    						+ traitementXMLAttribut(attributes.getValue(i)) + "\" ";
    			}
    			// System.out.println(attribut);
     
    			line += attribut.trim();
    		}
    		line += ">";
    		return line;
    	}
     
    	public boolean isFacture() {
    		return facture;
    	}
     
    	public void setFacture(boolean facture) {
    		this.facture = facture;
    	}
     
    	public boolean isFadet() {
    		return fadet;
    	}
     
    	public void setFadet(boolean fadet) {
    		this.fadet = fadet;
    	}
     
    	public boolean isGestionnaire() {
    		return gestionnaire;
    	}
     
    	public void setGestionnaire(boolean gestionnaire) {
    		this.gestionnaire = gestionnaire;
    	}
     
    	public BeanSFR getSfrBean() {
    		return sfrBean;
    	}
     
    	public void setSfrBean(BeanSFR sfrBean) {
    		this.sfrBean = sfrBean;
    	}
     
    	public boolean isTitulaire() {
    		return titulaire;
    	}
     
    	public void setTitulaire(boolean titulaire) {
    		this.titulaire = titulaire;
    	}
     
    	// public String test(String line) {
    	// StringBuffer buffer = new StringBuffer();
    	//
    	// for (int i = 0; i < line.length(); i++) {
    	// char[] tmp = { line.charAt(i) };
    	// String tmpStr = new String(tmp);
    	// tmpStr.replace('\r', ' ');
    	// tmpStr.replace('\n', ' ');
    	// buffer.append(tmpStr);
    	// }
    	// // System.out.println("buffer "+buffer.toString());
    	// return buffer.toString().trim();
    	//
    	// }
     
    	/**
             * 
             * @param _name
             * @return
             */
    	public String traitementXMLAttribut(String _name) {
    		StringBuffer buffer = new StringBuffer();
    		// String tmp = "";
    		for (int i = 0; i < _name.length(); i++) {
    			char[] tmp = { _name.charAt(i) };
    			String tmpStr = new String(tmp);
    			if (tmpStr.indexOf("<") != -1) {
    				String prefixe = tmpStr.substring(0, tmpStr.indexOf("<"));
    				String suffixe = tmpStr.substring(tmpStr.indexOf("<") + 1,
    						tmpStr.length());
    				tmpStr = prefixe + "&lt;" + suffixe;
    			} else if (tmpStr.indexOf(">") != -1) {
    				String prefixe = tmpStr.substring(0, tmpStr.indexOf(">"));
    				String suffixe = tmpStr.substring(tmpStr.indexOf(">") + 1,
    						tmpStr.length());
    				tmpStr = prefixe + "&gt;" + suffixe;
    			} else if (tmpStr.indexOf("&") != -1) {
    				String prefixe = tmpStr.substring(0, tmpStr.indexOf("&"));
    				String suffixe = tmpStr.substring(tmpStr.indexOf("&") + 1,
    						tmpStr.length());
    				tmpStr = prefixe + "&amp;" + suffixe;
    			} else if (tmpStr.indexOf("'") != -1) {
    				String prefixe = tmpStr.substring(0, tmpStr.indexOf("'"));
    				String suffixe = tmpStr.substring(tmpStr.indexOf("'") + 1,
    						tmpStr.length());
    				tmpStr = prefixe + "&apos;" + suffixe;
    			}
    			buffer.append(tmpStr);
    		}
    		// System.out.println(buffer.toString());
    		return buffer.toString();
    	}
    }
    Merci de le regarder

  15. #15
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 251
    Points
    1 251
    Par défaut
    Après tout, on est là pour faire de l'optimisation...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sfrBean.setDtd(str.substring(0, str.indexOf(">")) + ">");
    devient
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sfrBean.setDtd(str.substring(0, str.indexOf(">")+1));
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String str = new String(new String(ch));
    devient
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String str = new String(ch);
    Moi à ta place, je changerai mes déclarations ainsi
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    	   private StringBuffer strLineFac = null; 
    	   private StringBuffer strLineTitu = null; 
    	   private StringBuffer strLineGest = null; 
     
    	   public Handler() { 
    	      sfrBean = new BeanSFR(); 
    	      strLineFac=new StringBuffer();
    	      strLineTitu=new StringBuffer();
    	      strLineGest=new StringBuffer();
    	   }
    pour ensuite supprimer les += sur tes String
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    strLineGest += new String(ch, start, length);
    devient
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    strLineGest.append(new String(ch, start, length));
    et récupérer des String à la fin du traitement avec strLineGest.toString().

    Quand tu déclares une méthode throws une exception, ne faut il pas mettre un throw quelque part dans son code ? (cf endDocument() et endElement())

    Vu que la méthode charAt() appliquée à un String renvoie un et un seul char, et vu qu'il ne vaut mieux pas faire de déclaration dans une boucle, j'aurais fait le code suivant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for (int i = 0; i < _name.length(); i++) { 
             char[] tmp = { _name.charAt(i) }; 
             String tmpStr = new String(tmp);
    ainsi
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    String tmpStr=null;
    for (int i = 0; i < _name.length(); i++) { 
             tmpStr = _name.subString(i-1,i+1);
    et j'aurais également sorti les déclaration de String prefixe et String suffixe de la boucle. Le problème c'est qu'avec l'effet de charAt, et donc une chaine tmpStr de longueur 1, j'ai du mal à voir l'intérêt du traitement qui suit...
    Résultat j'aurais écrit cette méthode ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    		public String traitementXMLAttribut(String _name) { 
    	      StringBuffer buffer = new StringBuffer(); 
    	      char[] str=_name.toCharArray();
    	      for(int i=0;i<str.length;i++){
    	      	if(str[i]=='<')
    	      		buffer.append("&lt;");
    	      	else if(str[i]=='>')
    	      		buffer.append("&gt;");
    	      	else if(str[i]=='\'')
    	      		buffer.append("&apos;");
    	      	else if(str[i]=='&')
    	      		buffer.append("&amp;");
    	      	else
    	      		buffer.append(str[i]);
    	      }
    	      return buffer.toString();
    		}
    PS: C'est sûr qu'avec une JVM plus récente un petit replaceAll() aurait été le bienvenue
    PS2: J'aurais pas eu plus de mal à comprendre le code si il y avait eu plus de commentaires et moins de parties commentées

Discussions similaires

  1. Découper un fichier xml à l'aide de ksh
    Par azahiri dans le forum Linux
    Réponses: 11
    Dernier message: 19/08/2011, 18h06
  2. [XML] Découper un fichier XML
    Par Merfolk dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 11/04/2009, 19h32
  3. Script awk ou ksh pour découper un fichier xml
    Par Griffith dans le forum Shell et commandes GNU
    Réponses: 2
    Dernier message: 15/05/2007, 22h58
  4. [RegEx] Découper un fichier xml
    Par Dargos dans le forum Langage
    Réponses: 1
    Dernier message: 10/04/2006, 15h28
  5. Optimiser l'affichage d'un fichier XML de grosse taille...
    Par UnPeuPerdu dans le forum XML/XSL et SOAP
    Réponses: 11
    Dernier message: 03/06/2004, 16h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo