androidxml-parsingxmlpullparser

Parsing XML using XmlPullParser for specific tags?


Hey I have just started work on XML parsing in android. I am working on xml which has a structure like this.

        <quran>
        <sura index="113" name="الفلق">
                <aya index="1" text="قُلْ أَعُوذُ بِرَبِّ الْفَلَقِ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ" />
                <aya index="2" text="مِنْ شَرِّ مَا خَلَقَ" />
                <aya index="3" text="وَمِنْ شَرِّ غَاسِقٍ إِذَا وَقَبَ" />
                <aya index="4" text="وَمِنْ شَرِّ النَّفَّاثَاتِ فِي الْعُقَدِ" />
                <aya index="5" text="وَمِنْ شَرِّ حَاسِدٍ إِذَا حَسَدَ" />
            </sura>
            <sura index="114" name="الناس">
                <aya index="1" text="قُلْ أَعُوذُ بِرَبِّ النَّاسِ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ" />
                <aya index="2" text="مَلِكِ النَّاسِ" />
                <aya index="3" text="إِلَهِ النَّاسِ" />
                <aya index="4" text="مِنْ شَرِّ الْوَسْوَاسِ الْخَنَّاسِ" />
                <aya index="5" text="الَّذِي يُوَسْوِسُ فِي صُدُورِ النَّاسِ" />
                <aya index="6" text="مِنَ الْجِنَّةِ وَالنَّاسِ" />
            </sura>
        </quran>

Now in my first class I just want to show a list view in which just name of a sura is mentioned, which I have done successfully using this code.

 XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);

        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
                list.add(parser.getAttributeValue(0) + ".\n" + parser.getAttributeValue(1));
            }
            parser.next();
        }

In my lisview only name of sura is showing now I want to show the aya tags of specific sura on which user will click. In my next activity I am using this code

 XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);

        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("aya")) {
                list.add(parser.getAttributeValue(0) + ".\n" + parser.getAttributeValue(1));
            }
            parser.next();
        }

But I am getting all aya tags attributes instead of specific sura. Please help me to get just those aya which is present on a specific tag on which user will clicked. I just posted 2 tags of sura in xml actually i have 114 so please help me to use the right code. Sorry I am not good in english. Just help me out. Thanks :(


Solution

  • If you want to get all aya tags inside given sura, then you should write a method that take sura index as input for example 113 or 114 and return all aya tags as output.

    private List<String> getAllAyaFromSuraIndex(String suraIndex) {
        List<String> list = new ArrayList<>();
    
        XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);
        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
                String index = parser.getAttributeValue(0);
    
                // Match given sure index
                if (index.equals(suraIndex)) {
                    // Move to first aya tag inside matched sura index
                    parser.next();
    
                    // This loop will exit when it reaches sura end tag </sura>
                    while (parser.getName().equals("aya")) {
                        if (parser.getEventType() == XmlPullParser.START_TAG) {
                            list.add(parser.getAttributeValue(0) + ".\n" + parser.getAttributeValue(1));
                        }
    
                        // Move to next aya tag
                        parser.next();
                    }
    
                    break;
                }
            }
    
            parser.next();
        }
    
        return list;
    }