I am getting xml in a object back from a soap service.

<data><name>Test</name><name>Test</name><name>Test</name></data>

I need to convert that into an array so i can input it into a list

String[] accounts = result(this is my object);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.listaccounts, accounts));

How can i do this.

share|improve this question

56% accept rate
possible duplicate of How to parse this XML using Java? – Brian Roach Nov 28 '11 at 8:43
You need to parse the XML. Googling will give you lots of sample codes. – Sarwar Erfan Nov 28 '11 at 8:43
Sorry thought there might be something like result.toarray(). I am a newbie to java – Jed Nov 28 '11 at 9:15
feedback

3 Answers

up vote 1 down vote accepted

If the XML structure is so simple then regular expression will solve it for you.

private static final Pattern pattern = Pattern.compile("<name>([^<]+)</name>");
.....
Matcher m = pattern.compile(xmlString);
List<String> retList = new ArrayList<String>();
while(m.find()) {
    retList.add(m.group(1));
}
return retList;

It could be parsed even faster with String.indexOf();

Oh yeah: boatloads of people will tell you to use XML parser. It's complete overkill for simple XML. As long as it's not nested and you're not interested in attributes etc... simpler methods will do just fine.

share|improve this answer
1  
Oh yeah: boatloads of people will tell you to use XML parser. It's complete overkill for simple XML. As long as it's not nested and you're not interested in attributes etc... simpler methods will do just fine. – U Mad Nov 28 '11 at 8:48
But a real SOAP service response XML will not be as simple as posted..! – Mudassir Nov 28 '11 at 8:58
Well, he didn't say that, so that's an assumption. I've often dealt with soap calls which returned what's essentially a list of strings in simple xml. Hence why I said this method is for simple cases only. – U Mad Nov 28 '11 at 9:17
The poster has clearly written a object back from a soap service so its not an assumption. BTW, the poster has accepted your answer, so it seems that he/she got what he/she was looking for. – Mudassir Nov 28 '11 at 9:23
feedback

You can use for it xml parsers. Take a look at this topic

share|improve this answer
feedback

you have to parse the XML response and then fill the String array. For XML parsing, you can use the SAXParser.

Hope this helps!

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.