Erreur d'un parsing xml pour extraire une liste
Bonjour à tous,
J'essaie en vain de résoudre mon problème, mais sans succès. Surtout que je ne comprends pas d'où vient l'erreur.
Voici mon xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <compounds>
<compound name="alkene">
<product>Alkane</product>
<product>Alcohol</product>
<product>Halogenoalkane</product>
<product>Epoxide</product>
<product>Diol</product>
</compound>
<compound name="alkyne">
<product>Alkane</product>
<product>Alkene</product>
<product>Aldehyde/Ketone</product>
<product>Halogenoalkane</product>
<product>Carboxylic acid</product>
</compound>
</compounds> |
J'ai suivi ce tuto qui marche très bien pour un autre parsing.
Mon objectif est que pour chaque "compound", je puisse extraire une liste de "product" associés.
J'ai donc créer un bean Compound
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
| public class Compound {
private String name;
private ArrayList<String> products;
public Compound() {
super();
}
public Compound(String name, ArrayList<String> products) {
super();
this.name = name;
this.products = products;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getProducts() {
return products;
}
public void setProducts(ArrayList<String> products) {
this.products = products;
}
} |
et le Handler
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
| package com.turvy.organicreaction.xml;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
import com.turvy.organicreaction.beans.Compound;
public class XmlCompoundHandler extends DefaultHandler {
private List<Compound> compounds;
private String tempVal;
private Compound tempCompound;
private ArrayList<String> products;
// constructor
public XmlCompoundHandler() {
compounds = new ArrayList<Compound>();
;
}
public List<Compound> getCompounds() {
return compounds;
}
// start of the XML document
public void startDocument() {
Log.i("XmlCompoundHandler", "Start of XML document");
}
// end of the XML document
public void endDocument() {
Log.i("XmlCompoundHandler", "End of XML document");
}
// opening element tag
public void startElement(String uri, String name, String qName,
Attributes atts) {
// reset
tempVal = "";
// find out if the element is a brand
if (qName.equalsIgnoreCase("compound")) {
// create a new instance of compound
tempCompound = new Compound();
tempCompound.setName(atts.getValue("name"));
}
}
// closing element tag
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("compound")) {
// add it to the list
compounds.add(tempCompound);
} else if (qName.equalsIgnoreCase("product")) {
products.add(tempVal);
tempCompound.setProducts(products);
}
}
// element content
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
} |
Je n'ajoute pas le parser ni l'activité car ils ne sont pas en cause.
Le problème semble se situer au niveau de la méthode endElement puisque si j'omets le "else if (qName.equalsIgnoreCase("product")) {***}", alors ma liste de "Compound" est bien créée comme il faut. Dans le cas contraire, rien n'est fait et j'ai alors un NullPointerException dans mon activité.
J'aimerais donc savoir comment résoudre le problème et pourquoi problème il y a.