0

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...

0

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry I forgot to said that i put ArrayList<Client> clientslist = null; before the whole code i posted, so i replace client to clients(which is an array) and it makes an error i have to change to string)
Ok. Replace clients = text.trim(); with clientslist.add(new Client(text.trim())); to populate clientslist. Then you can read from clientslist as List or get an array of it's values via clientslist.toArray(new Client[clientslist.size()])
P.S. And dont initialise with null. Have List<Client> clientslist = new ArrayList<Client>(); instead.
1

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.

Comments

0

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();
        }
    }

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.