2013-12-10 4 views
0

나는 내가로부터 값을 얻을 필요가 내 응용 프로그램에서 XML 파일이 값이 파일의 일부 예제 코드입니다 ...PASE 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!"/> 
      <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> 

이 콘텐츠에서 나는 모든 aya 요소 텍스트 값을 sura에서 인덱스 i로 가져와야합니다. 누구든지이 일을 성취하도록 도와 줄 수 있습니까?

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

1 단계 : http://www.w3schools.com/xpath/xpath_syntax.asp

2 단계 : 당함 자신에게 더 좋은 일부 XPath를 알아보기 XPath 식을 좀 더 구체적으로 작성하시기 바랍니다 지금까지 내가 나에게 모든 아야 텍스트 값을 제공이 코드를 수행 한 . 나는 "// aya ​​[@ index = '1']"이 효과가있을 것이라고 생각합니다.

+0

힌트를 보내 주셔서 감사합니다. 분명히 xPath 표현식을보다 구체적으로 만들 수 있습니다. 감사합니다 –

0

이 문서의 문제를 해결 한 올바른 xPath는 quran/sura[" + (position + 1) + "]/*입니다. 이것은 모두 quran'ssura 색인을 가진 아이들 position+1 //starts from 1 not 0 아이를 의미합니다.

+0

요소의 속성이 변경되면 (위치 + 1) 후회할 것입니다. 나는 당신에게 말하고있다. @index는 갈 길이다. –