i don't know why I can't post code.
this is xml file
<employees>
<detail>
<img name="jame">jame_1</img>
<img name="jame">jame_2</img>
<img name="jame">jame_3</img>
<img name="natalie">natalie_1</img>
<img name="natalie">natalie_2</img>
<img name="phil">phil_1</img>
<img name="phil">phil_2</img>
<img name="phil">phil_3</img>
<img name="phil">phil_4</img>
</detail>
this is xmlpullparser to get name of img
public class trangaXmlpullparser {
List<name_emp> name_empList;
private name_emp n_e;
private String text;
public trangaXmlpullparser() {
name_empList = new ArrayList<name_emp>();
}
public List<name_emp> getName_empList() {
return name_empList;
}
public List<name_emp> parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
String attr = parser.getAttributeValue(null, "name");
if (tagname.equalsIgnoreCase("img")) {
n_e = new name_emp();
n_e.setName(attr);
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("img")) {
name_empList.add(n_e);
}
break;
default:
break;
}
eventType = parser.next();
}
}catch (Exception e) {
e.printStackTrace();
}
return name_empList;
}
}
so. I will get all name of img like image I post.
I just want get jame, natalie, phil in listview.
may be group attribute same value in to one. I don't know how to description it.
can xmlpullparser do it? and how to do that?
thank you for reading.
update .
this is result I follow Bhargav
this is right. but not perfect.
I want listview is
jame
natalie
phil
public class trangaXmlpullparser {
List<name_emp> name_empList;
private name_emp n_e;
private String text;
public trangaXmlpullparser() {
name_empList = new ArrayList<name_emp>();
}
public List<name_emp> getName_empList() {
return name_empList;
}
public List<name_emp> parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
String lastAttr = ""; // Initialize a string variable to hold the last attr you passed to setName.
String attr = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
attr = parser.getAttributeValue(null, "name");
if (tagname.equalsIgnoreCase("img")) {
n_e = new name_emp();
if(!attr.equals(lastAttr)) { // check if the attr you just parsed now is equal to the last unique attr you parsed
n_e.setName(attr); // if its not equal that means this attr is not a duplicate and you can set the name
lastAttr = attr; // update the last attr variable to hold the last attr you just assigned to n_e.
}
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("img") && !attr.equals(lastAttr)) {
name_empList.add(n_e);
}
break;
default:
break;
}
eventType = parser.next();
}
}catch (Exception e) {
e.printStackTrace();
}
return name_empList;
}
}
What I can suggest for your requirement is this,
n_e.setName
method and update the lastAttr
variable, the code snippet is above along with comments.