Nemka
Nemka

Reputation: 100

parse an XML to a Stringarray

I'm looking for help to get an XML to an arraylist.

here is the XML :

<campagne>
  <sms><texte>
          Vente a Drouot
    </texte>
  <list>
    <id> 1 </id>
      <nom> TOTO </nom>
    <id> 2 </id>
      <nom> TATA </nom>
    <id> 3 </id>
      <nom> Mr.Gerard </nom>
   </list>
</sms>
</campagne>

I want to have TOTO,TATA,Mr.Gerard to a StringArray[] exactly like if I put manually : String[] Customers = {"TOTO","TATA","Mr.Gerard"}

for now my XMlPullParser (I have written " ArrayList clientslist = null; " before and I want to put the difference name in this Array ) :

public void parseXMLAndStoreIt(XmlPullParser myParser) {
        int event;
        String text = null;
ArrayList<Client> clientslist = null;

        try {
            event = myParser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                String name = myParser.getName();

                switch (event) {
                case XmlPullParser.START_TAG:
                    break;
                case XmlPullParser.TEXT:
                    text = myParser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (name.equals("texte"))
                        message = text.trim();

                    else if (name.equals("nom")) 
                        clientslist = text.trim(); // error is here

                    else 

                    break;
                }
                event = myParser.next();

            }
            parsingComplete = false;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

With this code I have Mr.Gerard ONLY...

Upvotes: 0

Views: 123

Answers (4)

枫籽_
枫籽_

Reputation: 1

public ArrayList<String> parseXMLAndStoreIt(XmlPullParser myParser) {
        ArrayList<String> clientslist = new ArrayList<String>;       // 1
                  ...
                    else if (name.equals("nom")){
                        clients = text.trim();
                        clientslist.add(text.trim());     //2
                    }
                    ...
        return clientslist;    //3
    }

at the end , you can get the list

Upvotes: 0

Reji
Reji

Reputation: 3516

Use index for the string array to update the values.

public void parseXMLAndStoreIt(XmlPullParser myParser) {
        int event;
        int counter = 0;
        String text = null;
        String[] clients = {};

        try {
            event = myParser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                String name = myParser.getName();

                switch (event) {
                case XmlPullParser.START_TAG:
                    break;
                case XmlPullParser.TEXT:
                    text = myParser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (name.equals("texte"))
                        message = text.trim();

                    else if (name.equals("nom")) 
                        clients[counter++] = text.trim();

                    else 

                    break;
                }
                event = myParser.next();

            }
            parsingComplete = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

stridecolossus
stridecolossus

Reputation: 1561

What is the clients member variable? Without seeing the rest of the code it appears that you are simply over-writing it with whatever happens to be the last <nom> element.

Use a List and add each <nom> to that and then convert the list to a string array.

Upvotes: 1

mvmn
mvmn

Reputation: 4057

I suppose clients is a String - it should be a List<String> instead and clients = text.trim(); should be replaced with clients.add(text.trim());

Then you can call clients.toArray(new String[clients.size()]) to get a String array you wanted.

Upvotes: 1

Related Questions