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 :

Parser un XML sous Android - Il rate des lignes ?


Sujet :

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

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    165
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2007
    Messages : 165
    Par défaut Parser un XML sous Android - Il rate des lignes ?
    Bonjour,

    Je fais une application sous android et il récupère des XML que j'ai upload.
    Le problème étant que parfois le programme crash car il n'arrive pas a convertir un '' en integer, ce qui est logique. Mais dans mes XML il n'y a pas de vide et ils se lisent correctement depuis une application JDOM que j'ai fait (mais qui tourne sur mon pc).
    Tout comme parfois il ne récupère pas l'attribue 'id' que les éléments ont dans mon XML mais parfois il le fait.
    Je précise qu'il ne parse qu'une seule fois chaque XML et les fichiers XML ne sont pas erronées.
    Voici un peu de code :
    un exemple XML pour ma classe Build :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?xml version="1.0" encoding="UTF-8"?>
    <builds>
    <build custom="false" heroId="3" id="1">
    <author>Newti</author>
    <basics>39;38;2;2;4;4</basics>
    <basicsInformation>-</basicsInformation>
    <cores>58;69;60;60;64;89</cores>
    <coresInformation>TEXT</coresInformation>
    <luxuries>105;116;101</luxuries>
    <luxuriesInformation>TEXT</luxuriesInformation>
    </build>
    Le problème est quand il veut parser les integer et essayer de les mettre dans un tableau dynamique grâce d;abord a la classe de parsing :
    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
    package  ch.rxp.android.xml;
     
    import java.util.List;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    import ch.rxp.android.honbuilds.Build;
    import ch.rxp.android.honbuilds.Hero;
    import ch.rxp.android.honbuilds.HoNBuildsActivity;;
     
     
    public class BuildsHandler extends DefaultHandler{
     
    	// ===========================================================
         // Fields
         // ===========================================================
     
         private boolean in_builds = false;
         private boolean in_build = false;
         private boolean in_author = false;
         private boolean in_nbBuild = false;
         private boolean in_basics = false;
         private boolean in_basicsInformation = false;
         private boolean in_cores = false;
         private boolean in_coresInformation = false;
         private boolean in_luxuries = false;
         private boolean in_luxuriesInformation = false;
         private Build myBuild = null;
         private List<Build> listBuilds;
     
         // ===========================================================
         // constructor
         // ===========================================================
         public BuildsHandler(List<Build> listBuilds){
        	 this.listBuilds = listBuilds;
         }
     
         // ===========================================================
         // Getter & Setter
         // ===========================================================
     
         public List<Build> getParsedData() {
              return this.listBuilds;
         }
     
         // ===========================================================
         // Methods
         // ===========================================================
         @Override
         public void startDocument() throws SAXException {
              this.myBuild = new Build();
         }
     
         @Override
         public void endDocument() throws SAXException {
              // Nothing to do
         }
     
         /** Gets be called on opening tags like:
          * <tag>
          * Can provide attribute(s), when xml was like:
          * <tag attribute="attributeValue">*/
         @Override
         public void startElement(String namespaceURI, String localName,
                   String qName, Attributes atts) throws SAXException {
              if (localName.equals("builds")) {
                   this.in_builds = true;
              }else if (localName.equals("build")) {
                   this.in_build = true;
                   this.myBuild.setCustom(Boolean.parseBoolean(atts.getValue("custom")));
                   this.myBuild.setHeroId(Integer.parseInt(atts.getValue("heroId")));
                   this.myBuild.setId(Integer.parseInt(atts.getValue("id")));
              }else if (localName.equals("author")) {
                   this.in_author = true;
              }else if (localName.equals("nbBuild")) {
            	  this.in_nbBuild = true;
              }else if(localName.equals("basics")){
            	  this.in_basics = true;
              }else if(localName.equals("basicsInformation")){
            	  this.in_basicsInformation = true;
              }else if(localName.equals("cores")){
            	  this.in_cores = true;
              }else if(localName.equals("coresInformation")){
            	  this.in_coresInformation = true;
              }else if(localName.equals("luxuries")){
            	  this.in_basics = true;
              }else if(localName.equals("luxuriesInformation")){
            	  this.in_luxuriesInformation = true;
              }
         }
     
         /** Gets be called on closing tags like:
          * </tag> */
         @Override
         public void endElement(String namespaceURI, String localName, String qName)
                   throws SAXException {
        	 if (localName.equals("builds")) {
                 this.in_builds = false;
            }else if (localName.equals("build")) {
                 this.in_build = false;
                 this.listBuilds.add(this.myBuild);
                 this.myBuild = new Build();
            }else if (localName.equals("author")) {
                 this.in_author = false;
            }else if (localName.equals("nbBuild")) {
          	  this.in_nbBuild = false;
            }else if(localName.equals("basics")){
          	  this.in_basics = false;
            }else if(localName.equals("basicsInformation")){
          	  this.in_basicsInformation = false;
            }else if(localName.equals("cores")){
          	  this.in_cores = false;
            }else if(localName.equals("coresInformation")){
          	  this.in_coresInformation = false;
            }else if(localName.equals("luxuries")){
          	  this.in_basics = false;
            }else if(localName.equals("luxuriesInformation")){
          	  this.in_luxuriesInformation = false;
            }
         }
     
         /** Gets be called on the following structure:
          * <tag>characters</tag> */
         @Override
        public void characters(char ch[], int start, int length) {
              if(this.in_author){
            	  this.myBuild.setAuthor(new String(ch, start, length));
    	     } 
              if(this.in_basics){
            	  String[] basicsArray = (new String(ch,start,length).split("\\?"));
            	  for(int i = 0;i<basicsArray.length;i++){
            		  myBuild.createBasicList(basicsArray[i]);
            	  }
    	     } 
              if(this.in_basicsInformation){
    	          this.myBuild.setBasicsInformation(new String(ch, start, length));
    	     }
              if(this.in_cores){
            	  String[] coresArray = (new String(ch,start,length).split("\\?"));
            	  for(int i = 0;i<coresArray.length;i++){
            		  myBuild.createCoreList(coresArray[i]);
            	  }
    	     } 
              if(this.in_coresInformation){
    	          this.myBuild.setCoresInformation(new String(ch, start, length));
    	     }
              if(this.in_luxuries){
            	  String[] luxuriesArray = (new String(ch,start,length).split("\\?"));
            	  for(int i = 0;i<luxuriesArray.length;i++){
            		  myBuild.createLuxuryList(luxuriesArray[i]);
            	  }
    	     } 
              if(this.in_luxuriesInformation){
    	          this.myBuild.setLuxuriesInformation(new String(ch, start, length));
    	     }
        }
    }
    Ensuite a la classe même de l'objet :
    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
    package ch.rxp.android.honbuilds;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
     
     
    import java.util.*;
     
    public class Build implements java.lang.Comparable{
        private int id;
        private int heroId;
        private boolean isCustom;
        private List<List<Integer>> basics = new ArrayList<List<Integer>>();
        private List<Boolean> basicIsBuild = new ArrayList<Boolean>();
        private List<List<Integer>> cores = new ArrayList<List<Integer>>();
        private List<Boolean> coreIsBuild = new ArrayList<Boolean>();
        private List<List<Integer>> luxuries = new ArrayList<List<Integer>>();
        private List<Boolean> luxuryIsBuild = new ArrayList<Boolean>();
        private String basicsInformation;
        private String coresInformation;
        private String luxuriesInformation;
        private String author;	
     
     
        /* -------------------------> Setter & getter <-------------------------- */
     
        public Build(boolean isCustom){
        	this.isCustom = isCustom;
        }
        public Build(){
     
        }
     
        public int getId(){
            return this.id;
        }
        public void setId(int id){
        	this.id = id;
        }
        /**/
        public int getHeroId(){
            return this.heroId;
        }
        public void setHeroId(int heroId){
           this.heroId = heroId;
        }
        public void setHeroId(String heroStr, List<Hero> heroes){
            Iterator it = heroes.iterator();
            while(it.hasNext()){
                    Hero hero = (Hero) it.next();
                    if(hero.getName().contentEquals(heroStr)){
                            this.heroId = hero.getID();
                            break;
                    }else{
                            this.heroId = -1;
                    }
            }
            
         }
        /**/
        public boolean isCustom(){
            return this.isCustom;
        }
        public void setCustom(boolean isCustom){
        	this.isCustom = isCustom;
        }
        /**/
        public List<List<Integer>> getBasics(){
            return this.basics;
        }
        
        public List<Boolean> getBasicIsBuild(){
            return this.basicIsBuild;
        }
        
        public void setBasics (List<List<Integer>> basics,List<Boolean> basicsBool){
            this.basicIsBuild.clear();
            this.basicIsBuild.addAll(basicsBool);
            this.basics.clear();
            this.basics.addAll(basics);
        }
        /* */
        public List<List<Integer>> getCores(){
            return this.cores;
        }
     
        public List<Boolean> getCoreIsBuild(){
        	return this.coreIsBuild;
        }
     
        public void setCores (List<List<Integer>> cores, List <Boolean> coresBool){
        	this.coreIsBuild.clear();
        	this.coreIsBuild.addAll(coresBool);
        	this.cores.clear();
            this.cores.addAll(cores);
        }
        /**/
        public List<List<Integer>> getLuxuries(){
            return this.luxuries;
        }
        
        public List<Boolean> getLuxuryIsBuild(){
            return this.luxuryIsBuild;
        }
        
        public void setLuxuries (List<List<Integer>> luxuries, List<Boolean> luxuriesBool){
            this.luxuryIsBuild.clear();
            this.luxuryIsBuild.addAll(luxuriesBool);
            this.luxuries.clear();
            this.luxuries.addAll(luxuries);
        }
        /**/
        public String getBasicsInformation(){
            return this.basicsInformation;
        }
        public void setBasicsInformation (String basicsInformation){
           this.basicsInformation = basicsInformation;
        }
        /**/
        public String getCoresInformation(){
            return this.coresInformation;
        }
        public void setCoresInformation (String coresInformation){
           this.coresInformation = coresInformation;
        }
        /**/
        public String getLuxuriesInformation(){
            return this.luxuriesInformation;
        }
        public void setLuxuriesInformation (String luxuriesInformation){
           this.luxuriesInformation = luxuriesInformation;
        }
        /**/
        public String getAuthor(){
            return this.author;
        }
        public void setAuthor(String author){
            this.author = author;
        }
        
        /**/
        public void createBasicList(String basicItems){
        	if(basicItems.startsWith("$")){
        		basicIsBuild.add(true);
        	}
        	List<Integer> basicList = new ArrayList<Integer>();
        	String[] strListInt = basicItems.split(";");
     
        	for(int i = 0;i < strListInt.length;i++){
        		basicList.add(Integer.parseInt(strListInt[i]));
        	}
        	basics.add(basicList);    	
        }
        /**/
        public void createCoreList(String coreItems){
            if(coreItems.startsWith("$")){
                    coreIsBuild.add(true);
            }
            List<Integer> coreList = new ArrayList<Integer>();
            String[] strListInt = coreItems.split(";");
            System.out.println(coreItems);
     
            for(int i = 0;i < strListInt.length;i++){
                    coreList.add(Integer.parseInt(strListInt[i]));
            }
            cores.add(coreList);            
        }
        /**/
        public void createLuxuryList(String luxuryItems){
        	if(luxuryItems.startsWith("$")){
        		luxuryIsBuild.add(true);
        	}
        	List<Integer> luxuryList = new ArrayList<Integer>();
        	String[] strListInt = luxuryItems.split(";");
     
        	for(int i = 0;i < strListInt.length;i++){
        		luxuryList.add(Integer.parseInt(strListInt[i]));
        	}
        	luxuries.add(luxuryList);    	
        }
     
        /**
         */
     
        public int compareTo(Object other) {
            String otherObject =
                    ((Build) other).getBasics()
                    +" "+
                    ((Build) other).getCores()
                    +" "+
                    ((Build) other).getLuxuries()
                    +" "+
                    ((Build) other).getAuthor();
            String thisObject =
    	        	this.getBasics()
    	            +" "+
    	            this.getCores()
    	            +" "+
    	            this.getLuxuries()
    	            +" "+
    	            this.getAuthor();
            if(otherObject.compareTo(thisObject)>0){
                return -1;            
            }
            else if(otherObject.compareTo(thisObject)==0){
                return 0;            
            }
            else{
                return 1;
            }
        }
     
     
    }
    L'erreur survient dans les méthodes(a l'endroit du parsing de String -> int) :
    createBasicList
    createCoreList
    createLuxuryList

    Voila avez-vous une idée de ce qui se passe ?

    Cordialement,

    rXp>!<

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    1 466
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 1 466
    Par défaut
    Avec Sax, la methode "characters" peut être appelée plusieurs fois à l'intérieur d'un même élement.
    Tu dois mettres dans un buffer les "char" que tu lis, jusqu'à trouver la fin de l'élément car là tu es sûr qu'il n'y a plus de caractères à lire.

    Donc ils faut que tu vires tous tes "set()" de la méthode "characters()" pour le mettre dans la methode "endElement"

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    165
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2007
    Messages : 165
    Par défaut
    Donc si j'ai bien compris je dois charger une variable string dans set() et l'appliquer a mon objet dans endelement() ?
    Comme cela ? Car en tout cas l'erreur persiste :
    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
    package  ch.rxp.android.xml;
     
    import java.util.List;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    import ch.rxp.android.honbuilds.Build;
    import ch.rxp.android.honbuilds.Hero;
    import ch.rxp.android.honbuilds.HoNBuildsActivity;;
     
     
    public class BuildsHandler extends DefaultHandler{
     
    	// ===========================================================
         // Fields
         // ===========================================================
     
         private boolean in_builds = false;
         private boolean in_build = false;
         private boolean in_author = false;
         private boolean in_basics = false;
         private boolean in_basicsInformation = false;
         private boolean in_cores = false;
         private boolean in_coresInformation = false;
         private boolean in_luxuries = false;
         private boolean in_luxuriesInformation = false;
     
         private String str_author = "";
         private String str_basics = "";
         private String str_basicsInformation = "";
         private String str_cores = "";
         private String str_coresInformation = "";
         private String str_luxuries = "";
         private String str_luxuriesInformation = "";
         private Build myBuild = null;
         private List<Build> listBuilds;
     
         // ===========================================================
         // constructor
         // ===========================================================
         public BuildsHandler(List<Build> listBuilds){
        	 this.listBuilds = listBuilds;
         }
     
         // ===========================================================
         // Getter & Setter
         // ===========================================================
     
         public List<Build> getParsedData() {
              return this.listBuilds;
         }
     
         // ===========================================================
         // Methods
         // ===========================================================
         @Override
         public void startDocument() throws SAXException {
              this.myBuild = new Build();
         }
     
         @Override
         public void endDocument() throws SAXException {
              // Nothing to do
         }
     
         /** Gets be called on opening tags like:
          * <tag>
          * Can provide attribute(s), when xml was like:
          * <tag attribute="attributeValue">*/
         @Override
         public void startElement(String namespaceURI, String localName,
                   String qName, Attributes atts) throws SAXException {
              if (localName.equals("builds")) {
                   this.in_builds = true;
              }else if (localName.equals("build")) {
                   this.in_build = true;
                   this.myBuild.setCustom(Boolean.parseBoolean(atts.getValue("custom")));
                   this.myBuild.setHeroId(Integer.parseInt(atts.getValue("heroId")));
                   this.myBuild.setId(Integer.parseInt(atts.getValue("id")));
              }else if (localName.equals("author")) {
                   this.in_author = true;
              }else if(localName.equals("basics")){
            	  this.in_basics = true;
              }else if(localName.equals("basicsInformation")){
            	  this.in_basicsInformation = true;
              }else if(localName.equals("cores")){
            	  this.in_cores = true;
              }else if(localName.equals("coresInformation")){
            	  this.in_coresInformation = true;
              }else if(localName.equals("luxuries")){
            	  this.in_basics = true;
              }else if(localName.equals("luxuriesInformation")){
            	  this.in_luxuriesInformation = true;
              }
         }
     
         /** Gets be called on closing tags like:
          * </tag> */
         @Override
         public void endElement(String namespaceURI, String localName, String qName)
                   throws SAXException {
        	 if (localName.equals("builds")) {
                 this.in_builds = false;
            }else if (localName.equals("build")) {
                 this.in_build = false;
                 this.listBuilds.add(this.myBuild);
                 this.myBuild = new Build();
            }else if (localName.equals("author")) {
                 this.in_author = false;
                 this.myBuild.setAuthor(this.str_author);
                 this.str_author = "";
            }else if(localName.equals("basics")){
          	  this.in_basics = false;
          	  String[] basicsArray = (this.str_basics.split("\\?"));
          	  for(int i = 0;i<basicsArray.length;i++){
          		  myBuild.createBasicList(basicsArray[i]);
          	  }
          	  this.str_basics = "";
            }else if(localName.equals("basicsInformation")){
          	  this.in_basicsInformation = false;
              this.myBuild.setBasicsInformation(this.str_basicsInformation);
              this.str_basicsInformation = "";
            }else if(localName.equals("cores")){
          	  this.in_cores = false;
          	  String[] coresArray = (this.str_cores.split("\\?"));
          	  for(int i = 0;i<coresArray.length;i++){
          		  myBuild.createCoreList(coresArray[i]);
      	  		}
          	  this.str_cores = "";
            }else if(localName.equals("coresInformation")){
          	  this.in_coresInformation = false;
          	 this.myBuild.setCoresInformation(this.str_coresInformation);
          	 this.str_coresInformation = "";
            }else if(localName.equals("luxuries")){
          	  this.in_basics = false;
          	String[] luxuriesArray = (this.str_luxuries.split("\\?"));
      		for(int i = 0;i<luxuriesArray.length;i++){
      			myBuild.createLuxuryList(luxuriesArray[i]);
      		}
      		this.str_luxuries = "";
            }else if(localName.equals("luxuriesInformation")){
          	  this.in_luxuriesInformation = false;
              this.myBuild.setLuxuriesInformation(this.str_luxuriesInformation);
              this.str_luxuriesInformation = "";
            }
         }
     
         /** Gets be called on the following structure:
          * <tag>characters</tag> */
         @Override
        public void characters(char ch[], int start, int length) {
              if(this.in_author){
            	  this.str_author = this.str_author + new String(ch, start, length);
    	     } 
              if(this.in_basics){
            	  this.str_basics = this.str_basics + new String(ch, start, length);
    	     } 
              if(this.in_basicsInformation){
            	  this.str_basicsInformation = this.str_basicsInformation + new String(ch, start, length);
    	     }
              if(this.in_cores){
            	  this.str_cores = this.str_cores + new String(ch, start, length);
    	     } 
              if(this.in_coresInformation){
            	  this.str_coresInformation = this.str_coresInformation + new String(ch, start, length);
    	     }
              if(this.in_luxuries){
            	  this.str_luxuries = this.str_luxuries + new String(ch, start, length);
            	  System.out.println("LUXURY="+this.str_luxuries);
    	     } 
              if(this.in_luxuriesInformation){
            	  this.str_luxuriesInformation = this.str_luxuriesInformation + new String(ch, start, length);
    	     }
        }
    }

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    1 466
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 1 466
    Par défaut
    Je suis pas sûr que ce soit la bonne façon de concaténer tes chaines :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     public void characters(char ch[], int start, int length) {
              if(this.in_author){
            	  this.str_author = this.str_author + new String(ch, start, length);
    	     }
    A mon avis, s'il y a au moins 2 lecture de charactères à l'intérieur d'un même élement, le start s'incrémente de la longueur de la chaine précédemment lue.
    Donc le "new String(ch, start, length)", ne convient pas.

    Bon tout ça, c'est de tête mais avec un débuggeur tu verrais facilement si la concaténation se passe bien.
    Sinon, mieux vaut utiliser un seul et unique StringBuffer pour cette opération niveau perf.
    Pas besoin de savoir si t'es en train de concaténer des "auteurs" ou autres, tu pourras le tester quand tu rencontre la balise de fin pour faire la bonne affectation.

  5. #5
    Membre chevronné Avatar de Mobius
    Profil pro
    none
    Inscrit en
    Avril 2005
    Messages
    463
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : none

    Informations forums :
    Inscription : Avril 2005
    Messages : 463
    Par défaut
    Voici un Handler abstrait à implémenter que j'ai écrit une fois afin de faciliter l'utilisation du DefaultHandler.

    Code Java : 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
    import java.util.ArrayList;
    import java.util.List;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public abstract class AbstractHandler extends DefaultHandler
    {
     
       private StringBuilder currentBuffer = null;
     
       private List<StringBuilder> buffers = new ArrayList<StringBuilder>();
     
       @Override
       public final void startElement(String uri, String localName, String qName, Attributes attributes)
          throws SAXException {
          super.startElement(uri, localName, qName, attributes);
          currentBuffer = new StringBuilder();
          buffers.add(currentBuffer);
     
          startElement(uri, localName, attributes);
       }
     
       @Override
       public final void endElement(String uri, String localName, String qName) throws SAXException {
          characters(currentBuffer.toString());
          try {
             currentBuffer = buffers.remove(buffers.size() - 1);
          } catch (Exception e) {
             currentBuffer = null;
          }
          endElement(uri, localName);
       }
     
       @Override
       public final void characters(char[] ch, int start, int length) throws SAXException {
          super.characters(ch, start, length);
          if (0 < length) {
             currentBuffer.append(ch, start, length);
          }
       }
     
       public abstract void startElement(String uri, String localName, Attributes attributes) throws SAXException;
     
       public abstract void characters(String str) throws SAXException;
     
       public abstract void endElement(String uri, String localName) throws SAXException;
    }

    En espérant que ça en aidera d'autres.

Discussions similaires

  1. [JDOM] Parser du xml sous forme de string avec JDOM
    Par Lord Yu dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 02/02/2010, 12h47
  2. Réponses: 9
    Dernier message: 27/08/2009, 13h36
  3. Réponses: 0
    Dernier message: 06/07/2009, 10h33
  4. Réponses: 0
    Dernier message: 06/07/2009, 10h33
  5. Parser du xml sous wxWidgets
    Par ToMs dans le forum wxWidgets
    Réponses: 5
    Dernier message: 15/12/2007, 17h02

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