Aller au contenu

[Résolu] PROBLEME PARSING XML


Recommended Posts

Bonjour tout le monde !!

Bon je débute un peu en programmation Android, et j'ai un projet à faire pour mes études. Mais je rencontre un problème (auquel je ne trouve aps de réponse....).

Je m'explique.

J'ai un fichier XML, très court.

<data>
   <section id='1'>SECTION 1</section>
   <area>AREA 1</area>
</data>

Je bosse sur Eclipse pour info *

Le but de la manoeuvre est d'afficher 'SECTION 1' et 'AREA 1' dans mon application (sur un tablette).

Jusqu'à présent, j'arrive à afficher mes balises (donc 'Section', 'area' et 'data' dans mon LogCat) mais je n'arrive pas à afficher le contenu de ces balises.....ni dans mon LogCat, ni dans l'application.

Voilà, j'espère que vous allez trouvez un petit truc à mon problème. Ca serait génial !! :)

(Je vous met tout mon code à la suite)

Ma classe XML_SAX_PARSER

public class XML_SAX_PARSER extends Activity {
   /** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  // start the parser and get back the Data object, which you can do whatever you want with
  cout("Etape1");
  // PARCOURIR LE FICHIER XML
  Data data = _parseXml();




}

private Data _parseXml()
{
// private int _parseXml() {
Data data = null;

  // sax stuff
  try
  {	
  SAXParserFactory spf = SAXParserFactory.newInstance();
  SAXParser sp = spf.newSAXParser();

  XMLReader xr = sp.getXMLReader();

  DataHandler mydataHandler = new DataHandler();
  xr.setContentHandler((ContentHandler) mydataHandler);

  /***********CHEMIN SUR LA TABLETTE***********/
  xr.parse(new InputSource(new FileInputStream("data/data.xml")));
  //cout( xr.getProperty("section").toString());
  }


  catch(ParserConfigurationException pce)
  {
 Log.e("SAX XML", "sax parse error", pce);
  }
  catch(SAXException se)
  {
 Log.e("SAX XML", "sax error", se);
  }
  catch(IOException ioe)
  {
 Log.e("SAX XML", "sax parse io error", ioe);
  }
  //cout(data.toString());
  return data;
//   return 0;
}



private void cout(String string)
{
 TextView Text = new TextView(this);
 Text.setText(string);
 setContentView(Text);
}
}

Ma classe Data

public class Data
{
  // I know this could be an int, but this is just to show you how it works
  public String sectionId;
  public String section;
  public String area;

  public Data()
  {
  }
}[/font][/size][/color]
[color=#000080][size=4][font=comic sans ms,cursive]

Et enfin, ma classe DataHandler

[/font][/size][/color]
[color=#000080][size=4][font=comic sans ms,cursive]public class DataHandler extends DefaultHandler
{
// booleans that check whether it's in a specific tag or not
  private boolean _inSection, _inArea;

  // this holds the data
  private Data _data;

  /**
   * Returns the data object
   */
  public String getData()
  {
 return _data.toString();
  }

  /**
   * DEBUT DU PARSING
   */
  public void startDocument() throws SAXException
  {
 _data = new Data();
  }

  /**
   * FIN DU PARSING
   */
  public void endDocument() throws SAXException
  {

  }

  /**
   * This gets called at the start of an element. Here we're also setting the booleans to true if it's at that specific tag. (so we
   * know where we are)
   *
   * DETECTION D'OUVERTURE DE BALISE
   */
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
  {
 if(localName.equals("section"))
 {
   _inSection = true;

   _data.sectionId = atts.getValue("id");
 }
 else if(localName.equals("area"))
 {
   _inArea = true;
 }
  }

  /**
   * Called at the end of the element. Setting the booleans to false, so we know that we've just left that tag.
   *
   * DETECTION DE FIN DE BALISE
   */
  public void endElement(String namespaceURI, String localName, String qName) throws SAXException
  {
 Log.v("endElement", localName);

 if(localName.equals("section"))
 {
   _inSection = false;
 }
 else if(localName.equals("area"))
 {
   _inArea = false;
 }
  }

  /**
   * Calling when we're within an element. Here we're checking to see if there is any content in the tags that we're interested in
   * and populating it in the Config object.
   *
   ***** DETECTION DE CARACTERES *****
   */
  public void characters(char ch[], int start, int length)
  {
 String chars = new String(ch, start, length);
 chars = chars.trim();

 if(_inSection)
 {
   _data.section = chars;
 }
 else if(_inArea)
 {
   _data.area = chars;
 }
  }
}[/font][/size][/color]
[color=#000080][size=4][font=comic sans ms,cursive]

Encore merci d'avance.

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...