2013-05-07 3 views
0

TextView.setText(String) 메서드에 문자열 값을 넣을 수 있도록 로컬 XML 파일을 구문 분석하려면 어떻게해야합니까? 내 로컬 XML 파일은 다음과 같이 보입니다 :안드로이드에서 색인 된 XML 구문 분석

<quran> 
<sura index="1" name="الفاتحة"> 
    <aya index="1" text="In the name of Allah, the Entirely Merciful, the Especially Merciful."/> 
    <aya index="2" text="[All] praise is [due] to Allah, Lord of the worlds -"/> 
    <aya index="3" text="The Entirely Merciful, the Especially Merciful,"/> 
    <aya index="4" text="Sovereign of the Day of Recompense."/> 
    <aya index="5" text="It is You we worship and You we ask for help."/> 
    <aya index="6" text="Guide us to the straight path -"/> 
    <aya index="7" text="The path of those upon whom You have bestowed favor, not of those who have evoked [Your] anger or of those who are astray."/> 
</sura> 
<sura index="2" name="البقرة"> 
    <aya index="1" text="Alif, Lam, Meem."/> 
    <aya index="2" text="This is the Book about which there is no doubt, a guidance for those conscious of Allah -"/> 
    <aya index="3" text="Who believe in the unseen, establish prayer, and spend out of what We have provided for them,"/> 
    <aya index="4" text="And who believe in what has been revealed to you, [O Muhammad], and what was revealed before you, and of the Hereafter they are certain [in faith]."/> 
    <aya index="5" text="Those are upon [right] guidance from their Lord, and it is those who are the successful."/> 

내가 특정 ayh 인덱스 번호에있는 텍스트 문자열을 특정 액세스 할 수 있도록하려면를하지만 모든 아야 객체 내에 있기 때문에 그것을 할 방법을 잘 모르겠어요 수라 객체에는 두 가지가 있습니다. 색인과 액세스하려는 텍스트입니다. 도와주세요!

+0

사용 XML 풀 파서 –

+0

I이 파일을 위해 특별히 그것에 대해 이동하는 방법을 어떤 생각 :

은 다음과 같이 당신이 그렇게 당신이 방법 XmlPullParser.getAttributeValue(null, "text")를 사용해야합니다 "아야"요소에서 "텍스트"속성이다 또는 좋은 튜토리얼? 한 번에 한 문자열에 액세스 할 수 있어야합니다. 검색과 같은 종류입니다. 사용자가 위치를 입력하면 TextView에 배치 된 문자열이 표시됩니다. –

+0

[이 튜토리얼] (http://www.ibm.com/developerworks/opensource/library/x-android/)을 좋아합니다. 그것은 모든 일반적인 파서를 검토하고 모두를 비교하므로 어떤 것이 당신에게 적합한 지 알 수 있습니다. –

답변

0

XmlPullParser을 사용하려면 [가급적이면] [문서] [1]의 클래스 개요에있는 예제를 살펴보십시오.

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
factory.setNamespaceAware(true); 
XmlPullParser xpp = factory.newPullParser(); 

xpp.setInput(new StringReader ("<quran><sura index=\"1\" name=\"الفاتحة\"><aya index=\"1\" text=\"In the name of Allah, the Entirely Merciful, the Especially Merciful.\"/><aya index=\"2\" text=\"[All] praise is [due] to Allah, Lord of the worlds -\"/><aya index=\"3\" text=\"The Entirely Merciful, the Especially Merciful,\"/><aya index=\"4\" text=\"Sovereign of the Day of Recompense.\"/><aya index=\"5\" text=\"It is You we worship and You we ask for help.\"/><aya index=\"6\" text=\"Guide us to the straight path -\"/><aya index=\"7\" text=\"The path of those upon whom You have bestowed favor, not of those who have evoked [Your] anger or of those who are astray.\"/></sura><sura index=\"2\" name=\"البقرة\"><aya index=\"1\" text=\"Alif, Lam, Meem.\"/><aya index=\"2\" text=\"This is the Book about which there is no doubt, a guidance for those conscious of Allah -\"/><aya index=\"3\" text=\"Who believe in the unseen, establish prayer, and spend out of what We have provided for them,\"/><aya index=\"4\" text=\"And who believe in what has been revealed to you, [O Muhammad], and what was revealed before you, and of the Hereafter they are certain [in faith].\"/><aya index=\"5\" text=\"Those are upon [right] guidance from their Lord, and it is those who are the successful.\"/></sura></quran>")); 
int eventType = xpp.getEventType(); 
while (eventType != XmlPullParser.END_DOCUMENT) { 
    if(eventType == XmlPullParser.START_DOCUMENT) { 
     Log.i(getClass().getName(), "Start document"); 
    } else if(eventType == XmlPullParser.START_TAG) { 
     Log.i(getClass().getName(), "Start tag "+xpp.getName()); 
     if(xpp.getName().equals("aya")){ 
      Log.i(getClass().getName(), "aya text: " + xpp.getAttributeValue(null, "text")); 
     } 
    } else if(eventType == XmlPullParser.END_TAG) { 
     System.out.println("End tag "+xpp.getName()); 
     Log.i(getClass().getName(), "End tag "+xpp.getName()); 
    } else if(eventType == XmlPullParser.TEXT) { 
     System.out.println("Text "+xpp.getText()); 
     Log.i(getClass().getName(), "Text "+xpp.getText()); 
    } 
    eventType = xpp.next(); 
} 
Log.i(getClass().getName(), "End document"); 
+0

고마워요! 당신의 솔루션은 저를 도왔습니다! 미안 긴 동안 '동안 멀리 있었다. :) –

관련 문제