1

I'm trying to retrieve information from a XML file located in raw resources which structure is this:

<?xml version="1.0" encoding="utf-8" ?> <languages> <language code="ab">Abkhazian</language> <language code="aa">Afar</language> <language code="af">Afrikaans</language> <language code="sq">Albanian</language> ... 

What I've got to do is fill a List with the values of the languages, like 'Abkhazian','Afar',... I tried using XMLPullParser, but I don't know how to retrieve the name of the language, since it's not a field. Any help will be appreciated.

2 Answers 2

1

So what about you will use normal string-array?

<string-array name="languages"> <item>Abkhazian</item> <item>Afar</item> <item>Afrikaans</item> </string-array> 

then call String[] langs = getResources().getStringArray(R.id.languages);

Or try to use classic W3C DOM Parser:

DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); Document doc; doc = f.newDocumentBuilder().parse(<xml>); NodeList langs = doc.getElementsByTagName("languages"); ArrayList<String> data = new ArrayList<String>(); String lang = null; for (int i = 0; i < langs.getLength(); i++) { Node n = langs.item(i); if (n.getFirstChild().getNodeType() == Node.TEXT_NODE) { lang = n.getFirstChild().getNodeValue(); data.add(lang); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Worked! I had to change "languages" for "language" in your code, but all worked as excepted. Thank you!
0

presuming that you have declared your parser as:

XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setInput(is, null); //'is' is the InputStream corresponding to your xml file 

once you reach a tag, you can retrieve the data stored within it using:

String language = parser.nextText(); 

Go through this tutorial for more details: http://xjaphx.wordpress.com/2011/10/16/android-xml-adventure-parsing-xml-data-with-xmlpullparser/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.