我的应用程序中有一个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: "We believe in Allah and the Last Day;" 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: "Make not mischief on the earth," they say: "Why, we only Want to make peace!""/>
<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: "Believe as the others believe:" They say: "Shall we believe as the fools believe?" 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: "We believe;" but when they are alone with their evil ones, they say: "We are really with you: We (were) only jesting.""/>
<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>
从这个内容中,我需要从索引为i的sura
中获得所有aya
元素的文本值。有人能帮我实现这一点吗。到目前为止,我已经完成了这段代码,它给了我所有的aya文本值:
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());
}
步骤1:为了更大的利益,学习一些XPath:http://www.w3schools.com/xpath/xpath_syntax.asp
第2步:使XPath表达式更加具体。我认为"//aya[@index='1']"可能有效。
解决此文档问题的正确xPath是quran/sura[" + (position + 1) + "]/*
。这意味着获取所有具有索引position+1 //starts from 1 not 0
子级的quran's
sura
子级。