簡體   English   中英

在Android中提取解析本地XML文件

[英]Pull Parsing local XML File in 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文件的外觀:我試圖一次訪問位於aya中的一個文本字符串。

<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>

如果我正確理解這一點,那么這里的問題是您的XML沒有價值。 但是,您確實有一些帶有屬性的標簽。

例如:

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

與以下內容不同:

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

這兩個中的較晚者可能與您的代碼一起使用。 我認為您想要讀取的是屬性text而不是標簽的值。

您將在此處找到有關如何獲取屬性的更多信息。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM