2013-05-07 30 views
0

我想解析使用拉解析本地XML的特定部分,但是,我不知道如何阅读这些部分。我使用的代码:拉解析Android中的本地XML文件

package com.example.xmltest; 


import java.io.IOException; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.content.res.XmlResourceParser; 
import android.os.Bundle; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      TextView myXmlContent = (TextView)findViewById(R.id.xml_tv); 
      String stringXmlContent; 

      stringXmlContent = getAllXML(); 
      myXmlContent.setText(stringXmlContent); 

     } 

     public String getAllXML(){ 

      Activity activity = this; 
      String str = ""; 

      Resources res = activity.getResources(); 
      XmlResourceParser xpp = res.getXml(R.xml.ensaheehint); 

      try { 
       xpp.next(); 
       int eventType = xpp.getEventType(); 
       System.out.println("eventType : " + eventType); 
       while (eventType != XmlPullParser.END_DOCUMENT) 
       { 
        if(eventType == XmlPullParser.START_DOCUMENT){ 
         str += "nXML Parsing Starting...n"; 
        } 
        else if(eventType == XmlPullParser.START_TAG) 
        { 

         eventType = xpp.next(); 
         if(eventType == XmlPullParser.TEXT){ 
          String text = xpp.getName(); 
          str += "**TEXT: "+text; 
         } 


        } 
        else if(eventType == XmlPullParser.END_TAG) 
        { 
         str += "nending tag: "+xpp.getName(); 
        } 
        else if(eventType == XmlPullParser.TEXT) 
        { 
         str += "nvalue : "+xpp.getText(); 
        } 
        eventType = xpp.next(); 
       } 
       str += "nnXML parsing Ending......"; 

     } catch (XmlPullParserException e) { 
       e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
     return str; 
    } 

    } 

下面是我的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> 

回答

0

如果我正确地理解了这个问题,那么这里的问题是您在XML中没有任何价值。然而,你确实有一些标签有属性。

例:

<aya index="6" text="Guide us to the straight path -"/> 

是不一样的:

<aya index="6">Guide us to the straight path -"</aya> 

这两个的后面可能会与您的代码工作。我想你想要的是读取属性text而不是标签的值。

您将会找到关于如何获取属性here的更多信息。

希望它有帮助。

+0

你说得对,我需要阅读每个aya的**文本属性**,但我需要以类似搜索的方式执行此操作。因此,举例来说,如果用户输入2:34,那么这意味着这将返回索引属性为34的aya的text属性中的第二个sura中存在的字符串。这是否使事情更清晰? – 2013-05-08 13:56:10

+0

啊哈,现在我明白了。检查sura节点的索引属性,如果它搜索aya节点的25个索引属性。我认为这应该是相当直接的。你似乎有一个很好的基础来建立。除非我错过了什么。 :-) – Qben 2013-05-08 18:57:38

+0

非常感谢,对不起一段时间了! – 2015-05-08 19:25:09