android xmlpullparser获取属性相同的值

时间:2016-02-23 03:33:28

标签: android xmlpullparser

我不知道为什么我不能发布代码 这是xml文件

<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>

这是获取img

名称的xmlpullparser
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;
    }
}

这是结果

enter image description here

如此。我将获得img的所有名称,就像我发布的图像一样 我只想在listview中得到jame,natalie,phil 可以是组属性相同的值到一个。我不知道如何描述它 可以xmlpullparser吗?以及如何做到这一点? 谢谢你的阅读。

更新。
这是我跟随巴尔加夫的结果 这是正确的。但并不完美。

enter image description here

我希望listview是
詹姆斯
娜塔莉
菲尔

2 个答案:

答案 0 :(得分:0)

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;
    }
}

我建议您的要求是这个,

  • 保存一个名为lastAttr的变量,该变量包含刚刚传递给n_e变量的最后一个属性值。
  • 如果您解析的detr不等于您持有的lastAttr,则将其传递给n_e.setName方法并更新lastAttr变量,上面的代码片段以及注释。

答案 1 :(得分:0)

public class trangaXmlpullparser {
    List<name_emp> name_empList;
    ArrayList<String> a = new Arraylist<>(); // Initialising new list
    boolean isDublicate = false; // and flag
    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")) {
                            if (!a.contains(attr)) { // Checking if our list of strings contains attribute
                                a.add(attr); // We add new attribute in our list of strings
                                n_e = new name_emp();
                                n_e.setName(attr);
                            } else {
                                isDublicate = true; // if attribute repeats, the flag becomes true
                            }
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("img")) {
                            if (!isDublicate) // Checking flag
                                name_empList.add(n_e); // If flag is false we add new element
                            else
                                isDublicate = false; // else we set flag to its default value
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return name_empList;
    }
}

如果你实现了name_emp的equals()方法,还有另一个更美妙的解决方案。您可以使用set(Set)替换ArrayList。 Set会自动检查添加的对象是否唯一(使用overriden equals()方法),所以你唯一应该做的就是将Set复制到ArrayList(new ArrayList.addAll(set))。