I am facing a problem in parsing the following XML:
<key>key 1</key>
<string>string 1</string>
<key>key 2</key>
<string>string 2</string>
<key>key 3</key>
<string>string 3</string>
I have written the following code but I get data only of the first tag in TextView, not the data of tags string2 and string 3. How to parse XML with same name tags?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context mContext = this;
TextView tv = (TextView) findViewById(R.id.info);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
AssetManager assetManager = mContext.getAssets();
InputStream inputStream = assetManager.open("abc.xml");
Document doc=db.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("string");
for (int i = 0; i < nodeList.getLength(); i++) {
tv.setText( "\n\n"+ nodeList.item(i).getChildNodes().item(i).getNodeValue()+"\n");
}
} catch (Exception e) {
System.out.println("Pasing Excpetion = " + e);
}
}