Pull Parsing local XML File in Android

北城余情 提交于 2019-12-12 03:53:25

问题


I'm trying to parse specific parts of a local XML using pull-parsing however, I'm not sure how I read those parts. I'm using the code:

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

  }

Below is what my XML file looks like: I'm trying to access one text string located in an aya at a time.

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

回答1:


If I understand this correctly the problem here is that you have no value in your XML. You do however have some tags with attributes in them.

Ex:

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

is not the same as:

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

The later of these two would probably work with your code. What I think you want is to read the attribute text and not the value of the tag.

You will find further information on how to fetch the attributes here.

Hope it helps.



来源:https://stackoverflow.com/questions/16419540/pull-parsing-local-xml-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!