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:
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:
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:
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>!<