问题
I want to Parse an XML which has data differentiated on the basis of id (can say in tabular form) and to show it in Textviews (one id at a time) Can you help me in this regard?? Preferably in XML PullParser as it is recommended by android
I want XML values to put in Textviews one at a time based on their IDs
<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>
回答1:
You can do something like ,
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;
}
}
Output :
<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
What i did was,
- Created a Model for Id, English Data, UruduData.
- Declared an
ArraList
with paramater asModel
. - Whenever it sees
myXmlStarts
as START_TAG, it just initialize the model class. (model = new Model();) - You just add to model once it finds the END_TAG as
Id
,EnglishData
etc.. - But, if it finds the END_TAG as
myXmlStarts
, it just adds that toArrayList
(listData.add(model);) and the same thing happens until the end of document.
Now, you have all the data in ArrayList
. Just add these values to textView
. I just finished your half job. Rest, you have to try. Good luck.
NOTE :
You have a error in your xml tags.
Change
<Id>1</id>
to
<Id>1</Id> <-- closing and opening tag name should be same. Not `i` once and 'I' later. Good luck.
来源:https://stackoverflow.com/questions/29442422/android-parsing-xml-to-textview