Android;解析XML到文本视图
问题描述:
我想解析一个基于id的数据有区别的XML(可以用表格的形式说明)并在文本视图中显示(一次一个ID) 在这方面你能帮助我吗? ?最好是在XML PullParser,因为它是由AndroidAndroid;解析XML到文本视图
我想XML值建议放在Textviews一次一个基于其ID
<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>
答
你可以做这样的事情,
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());
}
}
}
Model.class
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
我所做的就是,
- 创建的标识,英文资料,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.