javaandroidxmlandroid-parserandroid-pullparser

Parse XML from assets file


I have an xml file in the assets folder that I need to parse. Here is the structure of the xml:

<quran>
    <sura index="1" name="الفاتحة">
        <aya index="1" text="In the name of Allah, Most Gracious, Most Merciful."/>
        <aya index="2" text="Praise be to Allah, the Cherisher and Sustainer of the worlds;"/>
        <aya index="3" text="Most Gracious, Most Merciful;"/>
        <aya index="4" text="Master of the Day of Judgment."/>
        <aya index="5" text="Thee do we worship, and Thine aid we seek."/>
        <aya index="6" text="Show us the straight way,"/>
        <aya index="7" text="The way of those on whom Thou hast bestowed Thy Grace, those whose (portion) is not wrath, and who go not astray."/>
    </sura>
    <sura index="2" name="البقرة">
        <aya index="1" text="A. L. M."/>
        <aya index="2" text="This is the Book; in it is guidance sure, without doubt, to those who fear Allah;"/>
        <aya index="3" text="Who believe in the Unseen, are steadfast in prayer, and spend out of what We have provided for them;"/>
        <aya index="4" text="And who believe in the Revelation sent to thee, and sent before thy time, and (in their hearts) have the assurance of the Hereafter."/>
        <aya index="5" text="They are on (true) guidance, from their Lord, and it is these who will prosper."/>
        <aya index="6" text="As to those who reject Faith, it is the same to them whether thou warn them or do not warn them; they will not believe."/>
        <aya index="7" text="Allah hath set a seal on their hearts and on their hearing, and on their eyes is a veil; great is the penalty they (incur)."/>
        <aya index="8" text="Of the people there are some who say: &quot;We believe in Allah and the Last Day;&quot; but they do not (really) believe."/>
        <aya index="9" text="Fain would they deceive Allah and those who believe, but they only deceive themselves, and realise (it) not!"/>
    </sura>
</quran>

Can someone help me and tell me how can i parse this xml to get the index and the name from each sura element and index and text from each aya element.

For now I have only this code:

XmlPullParserFactory pullParserFactory;
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

                InputStream in_s = getApplicationContext().getAssets().open("filename.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);
        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Thanks


Solution

  • XPath xpath = XPathFactory.newInstance().newXPath();  
    String expression = "//sura";  
    InputSource inputSource = new InputSource("filename.xml");  
    NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);  
    
    for(int i=0; i<nodes.getLength(); i++) {  
        Node sura = nodes.item(i);  
        NamedNodeMap attributes = sura.getAttributes();  
        attributes.getNamedItem("name").getNodeValue()  
    }