简体   繁体   English

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

[英]Pull Parsing local XML File in Android

I'm trying to parse specific parts of a local XML using pull-parsing however, I'm not sure how I read those parts. 我正在尝试使用拉式解析来解析本地XML的特定部分,但是我不确定如何阅读这些部分。 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. 以下是我的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>

If I understand this correctly the problem here is that you have no value in your XML. 如果我正确理解这一点,那么这里的问题是您的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. 我认为您想要读取的是属性text而不是标签的值。

You will find further information on how to fetch the attributes here . 您将在此处找到有关如何获取属性的更多信息。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM