Domパーサーを使用して同じタグを解析するには、以下のコードを使用してください。
public class DomParserSampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView mScrView1 = new ScrollView(this);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView id[];
TextView published[];
TextView content[];
TextView title[];
TextView mediacontent[];
TextView mediathumbnail[];
try {
URL url = new URL("URL_OF_XML_FILE");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("dict");
/** Assign textview array length by arraylist size */
firstkey = new TextView[nodeList.getLength()];
secondkey = new TextView[nodeList.getLength()];
thirdkey = new TextView[nodeList.getLength()];
fourthkey = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
firstkey[i] = new TextView(this);
secondkey[i] = new TextView(this);
thirdkey[i] = new TextView(this);
fourthkey[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList keyList = fstElmnt.getElementsByTagName("key");
Element keyElement = (Element) keyList.item(0);
keyList = keyElement.getChildNodes();
firstkey[i].setText("key is = "
+ ((Node) keyList.item(0)).getNodeValue());
secondkey[i].setText("key is = "
+ ((Node) keyList.item(1)).getNodeValue());
thirdkey[i].setText("key is = "
+ ((Node) keyList.item(2)).getNodeValue());
fourthkey[i].setText("key is = "
+ ((Node) keyList.item(3)).getNodeValue());
layout.addView(firstkey[i]);
layout.addView(secondkey[i]);
layout.addView(thirdkey[i]);
layout.addView(fourthkey[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
/** Set the layout view to display */
mScrView1.addView(layout);
setContentView(mScrView1);
}
}