在Android的拉解析本地XML文件 [英] Pull Parsing local XML File in Android

查看:147
本文介绍了在Android的拉解析本地XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用却拉解析解析本地XML的特定部分,我不知道我是怎么看的部分。我使用的是code:

 包com.example.xmltest;
进口java.io.IOException异常;
进口org.xmlpull.v1.XmlPullParser;
进口org.xmlpull.v1.XmlPullParserException;
进口android.app.Activity;
进口android.content.res.Resources;
进口android.content.res.XmlResourceParser;
进口android.os.Bundle;
进口android.widget.TextView;公共类MainActivity延伸活动{
    @覆盖
      公共无效的onCreate(捆绑savedInstanceState){
          super.onCreate(savedInstanceState);
          的setContentView(R.layout.activity_main);          TextView的myXmlContent =(的TextView)findViewById(R.id.xml_tv);
          串stringXmlContent;          stringXmlContent = getAllXML();
          myXmlContent.setText(stringXmlContent);      }      公共字符串getAllXML(){          活动活动=这一点;
          字符串str =;          资源解析度= activity.getResources();
          XmlResourceParser XPP = res.getXml(R.xml.ensaheehint);          尝试{
              xpp.next();
              INT EVENTTYPE = xpp.getEventType();
              的System.out.println(EVENTTYPE:+ EVENTTYPE);
              而(EVENTTYPE!= XmlPullParser.END_DOCUMENT)
              {
                   如果(EVENTTYPE == XmlPullParser.START_DOCUMENT){
                       STR + =nXML解析启动... N的;
                   }
                   否则,如果(EVENTTYPE == XmlPullParser.START_TAG)
                   {                       EVENTTYPE = xpp.next();
                       如果(EVENTTYPE == XmlPullParser.TEXT){
                           字符串文本= xpp.getName();
                           STR + =TEXT **:+文字;
                       }
                   }
                   否则,如果(EVENTTYPE == XmlPullParser.END_TAG)
                   {
                       STR + =nending标签:+ xpp.getName();
                   }
                   否则,如果(EVENTTYPE == XmlPullParser.TEXT)
                   {
                       STR + =n值:+ xpp.getText();
                   }
                   EVENTTYPE = xpp.next();
              }
               STR + =nnXML解析结局......        }赶上(XmlPullParserException E){
              e.printStackTrace();
        }赶上(IOException异常五){
              e.printStackTrace();
        }
        返回海峡;
    }  }

下面是我的XML文件的样子:
我试图访问位于一个绫一次一个文本字符串。

 <&古兰经GT;
&所述;苏拉索引=1名称=الفاتحة>
    <绫指数=1文本=在真主的名义,一个完全仁慈的,尤其是在仁慈/>
    <绫指数=2文本=[全部]赞的是[适当]真主,全世界的主 - />
    <绫指数=3文本=经过全面仁慈的仁慈特别是,/>
    <绫指数=4文本= /&GT报应日的主权。
    <绫指数=5文本=这是你我们崇拜你,我们寻求帮助。/>
    <绫指数=6文本=引导我们正道 - />
    <绫指数=7文本=。这些人在你们赋予不是那些谁诱发[你]愤怒或那些谁是误入歧途的青睐,的路径/>
< /苏拉>


解决方案

如果我理解正确这这里的问题是,你有你的XML没有价值。然而,你有一些标签在他们的属性。

例如:

 <绫指数=6文本=引导我们正道 - />

是不一样的:

 <绫指数=6>指导我们的直线路径 - < /绫>

这两个的后面可能会与你的code工作。我想你想要的是读取属性文本和标记不是值。

您会找到如何这里获取属性的详细信息

希望它帮助。

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>

解决方案

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.

这篇关于在Android的拉解析本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆