你可以做类似的事情,
public class TestClass {
static String text;
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
List<Model> listData = new ArrayList<Model>();
Model model = null;
String xmlString = "<myXmlStarts><Id>1</Id><EnglishData>this is 1st English line</EnglishData><UrduData>this is 1st Urdu line</UrduData></myXmlStarts><myXmlStarts><Id>2</Id><EnglishData>this is 2nd English line</EnglishData><UrduData>this is 2nd Urdu line</UrduData></myXmlStarts>";
xpp.setInput(new StringReader (xmlString));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("myXmlStarts")) {
System.out.println("<myXmlStarts>");
model = new Model();
}
break;
case XmlPullParser.TEXT:
text = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("myXmlStarts")) {
System.out.println("</myXmlStarts>");
listData.add(model);
}
if (tagname.equalsIgnoreCase("Id")) {
System.out.println("Id is : " +text);
model.setId(text);
}
if (tagname.equalsIgnoreCase("EnglishData")) {
System.out.println("english data is : " +text);
model.setEngData(text);
}
if (tagname.equalsIgnoreCase("UrduData")) {
System.out.println("urdudata is : " +text);
model.setUrduData(text);
}
// ....add what are the tags you want
default:
break;
}
eventType = xpp.next();
} // end of while loop
System.out.println("Array list size : " +listData.size());
int listSize = listData.size();
for (int i = 0; i < listSize; i++) {
System.out.println("Id : " + listData.get(i).getId());
System.out.println("Id : " + listData.get(i).getEngData());
System.out.println("Id : " + listData.get(i).getUrduData());
}
}
}
模型类
public class Model {
String id, engData, urduData;
public String getEngData() {
return engData;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setEngData(String engData) {
this.engData = engData;
}
public String getUrduData() {
return urduData;
}
public void setUrduData(String urduData) {
this.urduData = urduData;
}
}
输出 :
<myXmlStarts>
Id is : 1
english data is : this is 1st English line
urdudata is : this is 1st Urdu line
</myXmlStarts>
<myXmlStarts>
Id is : 2
english data is : this is 2nd English line
urdudata is : this is 2nd Urdu line
</myXmlStarts>
Array list size : 2
Id : 1
Id : this is 1st English line
Id : this is 1st Urdu line
Id : 2
Id : this is 2nd English line
Id : this is 2nd Urdu line
我所做的是,
- 为 Id、English Data、UruduData 创建了一个模型。
ArraList
将带有参数声明为Model
.
- 每当它看到
myXmlStarts
START_TAG 时,它只是初始化模型类。(模型=新模型();)
- 一旦找到 END_TAG等
Id
,您只需添加到模型中EnglishData
。
- 但是,如果它发现 END_TAG 为
myXmlStarts
,它只是将它添加到ArrayList
(listData.add(model);) 并且同样的事情会发生,直到文档结束。
现在,您拥有ArrayList
. 只需将这些值添加到textView
. 我刚刚完成了你的一半工作。休息,你必须尝试。祝你好运。
笔记 :
您的 xml 标签中有错误。
改变
<Id>1</id>
至
<Id>1</Id> <-- closing and opening tag name should be same. Not `i` once and 'I' later. Good luck.