문제

I have an xml file in my app that i need to get value from, here is some example code of that file:

 <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!"/>
            <aya index="10" text="In their hearts is a disease; and Allah has increased their disease: And grievous is the penalty they (incur), because they are false (to themselves)."/>
            <aya index="11" text="When it is said to them: &quot;Make not mischief on the earth,&quot; they say: &quot;Why, we only Want to make peace!&quot;"/>
            <aya index="12" text="Of a surety, they are the ones who make mischief, but they realise (it) not."/>
            <aya index="13" text="When it is said to them: &quot;Believe as the others believe:&quot; They say: &quot;Shall we believe as the fools believe?&quot; Nay, of a surety they are the fools, but they do not know."/>
            <aya index="14" text="When they meet those who believe, they say: &quot;We believe;&quot; but when they are alone with their evil ones, they say: &quot;We are really with you: We (were) only jesting.&quot;"/>
            <aya index="15" text="Allah will throw back their mockery on them, and give them rope in their trespasses; so they will wander like blind ones (To and fro)."/>
            <aya index="16" text="These are they who have bartered Guidance for error: But their traffic is profitless, and they have lost true direction,"/>
            <aya index="17" text="Their similitude is that of a man who kindled a fire; when it lighted all around him, Allah took away their light and left them in utter darkness. So they could not see."/>
            <aya index="18" text="Deaf, dumb, and blind, they will not return (to the path)."/>
            <aya index="19" text="Or (another similitude) is that of a rain-laden cloud from the sky: In it are zones of darkness, and thunder and lightning: They press their fingers in their ears to keep out the stunning thunder-clap, the while they are in terror of death. But Allah is ever round the rejecters of Faith!"/>
            <aya index="20" text="The lightning all but snatches away their sight; every time the light (Helps) them, they walk therein, and when the darkness grows on them, they stand still. And if Allah willed, He could take away their faculty of hearing and seeing; for Allah hath power over all things."/>
            <aya index="21" text="O ye people! Adore your Guardian-Lord, who created you and those who came before you, that ye may have the chance to learn righteousness;"/>
            <aya index="22" text="Who has made the earth your couch, and the heavens your canopy; and sent down rain from the heavens; and brought forth therewith Fruits for your sustenance; then set not up rivals unto Allah when ye know (the truth)."/>
            <aya index="23" text="And if ye are in doubt as to what We have revealed from time to time to Our servant, then produce a Sura like thereunto; and call your witnesses or helpers (If there are any) besides Allah, if your (doubts) are true."/>
            <aya index="24" text="But if ye cannot- and of a surety ye cannot- then fear the Fire whose fuel is men and stones,- which is prepared for those who reject Faith."/>
         </sura>

...

From this content i need to get all aya elements text value from sura with index i. Can anyone help me achieve this. So far i've done this code that gives me all aya text values:

XPath xpath = XPathFactory.newInstance().newXPath();
                    String expression = "//aya";
                    // String expression = "//aya";
                    // sura index = position
                    InputSource inputSource = new InputSource(getAssets().open(
                            "en.yusufali.xml"));
                    NodeList nodes;

                    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();
                        ayas.add(attributes.getNamedItem("text").getNodeValue());
                    }
도움이 되었습니까?

해결책

Step 1: Got learn yourself some XPath for the greater good: http://www.w3schools.com/xpath/xpath_syntax.asp

Step 2: Make your XPath expression more specific. I think "//aya[@index='1']" may work.

다른 팁

The correct xPath that solved the problem for this document is quran/sura[" + (position + 1) + "]/* . This means get all quran's sura child with index position+1 //starts from 1 not 0 children.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top