XmlPullParser如何实现RES /生/ XML / xmlfilename? [英] XmlPullParser how to attain res/raw/xml/xmlfilename?

查看:101
本文介绍了XmlPullParser如何实现RES /生/ XML / xmlfilename?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来编程,所以开始用正确的我,如果我错了,在下面的段落:

I am new to programming, so to start with correct me if I am wrong in the paragragh below :

目前主要有三种XML解析器在Android中使用:SAX,DOM和XmlPullParser。这最后一个选项,而现有的作为外部的ressource。是核心的Andr​​oid,因此工作速度快,但functionnalities限于

There is mainly three xml parsers for use in Android : Sax, Dom, and XmlPullParser. That last option, while existing as an external ressource. Is "in the core" of Android, thus working faster, but the functionnalities are limited

确定这里是我的问题我稍微修改以下

Ok here is my Question I slightly modified the code provided in the link below

http://developer.android.com/reference/org/ xmlpull / V1 / XmlPullParser.html

http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

我做了以下内容:

import java.io.IOException;
import java.io.StringReader; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; 

public class xmlPParser
{   

    public String texte;
    public xmlPParser (String arg)         
      throws XmlPullParserException, IOException     
    {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();
         xpp.setInput( new StringReader ( arg ) );
         int eventType = xpp.getEventType();



         while (eventType != XmlPullParser.END_DOCUMENT)
       {
            //if(eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start document");} 
       // else if(eventType == XmlPullParser.START_TAG) { System.out.println("Start tag "+xpp.getName()); } 
        //else if(eventType == XmlPullParser.END_TAG) {  System.out.println("End tag "+xpp.getName()); }
        if(eventType == XmlPullParser.TEXT){ texte = xpp.getText();  }    //{ System.out.println("Text "+xpp.getText());}          

        eventType = xpp.next();
            }         

         //System.out.println("End document");
         } 

        public String getTexte()
        {
            String returnTexte = texte;
            return returnTexte;
        }

} 

在另一个的java文件,我可以调用分析器以下列方式:

In another java file, I can call the parser in the following way :

public xmlPParser myxpp;

...

myxpp = new xmlPParser("<foo>Hi five !!</foo>");

在最后一行:我想能问分析器中去,而不是传递一个字符串到一个文件。如何将我做到这一点? 我不知道如何利用此张贴 是否getResources()的getXML()假设我使用了Android pullParser这我不知道要使用呢?

On that last line : I would like to be able to ask the parser to go to a file, instead of passing a string to it. how would i do that ? I am not sure how to make use of this posting Does getResources().getXml() supposes I am using the Android pullParser which I am not sure to be using now ?

推荐答案

XmlPullParser是不是一个真正的解析器,它是一个接口类型的解析器,称为拉解析器。

XmlPullParser is not really a parser, it is an interface to a type of parser, called a "pull" parser.

函数getResources()的getXML()返回XmlPullParser的解析的XML资源的实现。这是不是一个真正的XML解析器 - 事实上,原始的XML文件解析在编译的时候被内置到应用程序之前,什么这个XML解析器正在做的是刚刚返回pre消化的XML结构,你调用其API。这是最快的XML解析器在Android(因为它不是真正的解析任何东西),但需要XML文档被编译为构建应用程序的一部分。

The function getResources().getXml() returns an implementation of XmlPullParser for "parsing" XML resources. This is not a real XML parser -- in fact the original XML file was parsed at build time before being built into your app, and what this "XML parser" is doing is just returning the pre-digested XML structure as you call its API. This is the fastest "XML" parser available on Android (because it is not really parsing anything), but requires that the XML document be compiled as part of building your app.

另外实施XmlPullParser,你从XmlPullParserFactory.newInstance()得到的是不是限制 - 这是实现全功能,并可以分析你给它的任何原始XML文档

The other implementation of XmlPullParser that you get from XmlPullParserFactory.newInstance() is not "limited" -- this implementation is full-featured, and can parse any raw XML document you give to it.

目前至少有一次(不知道这仍是如此),无论是SAX解析器和()通过XmlPullParserFactory.newInstance返回解析器实际上是建立在相同的底层实现,这是外籍人士。 Expat解析器是一个推解析器(即相同的模型作为SAX),所以使用它是与SAX API的最有效方式。该XmlPullParser版本已经从SAX一些开销,因为它需要把潜在的语义推到拉界面。

At least at one time (not sure if this is still the case), both the SAX parser and the parser returned by XmlPullParserFactory.newInstance() are actually built on the same underlying implementation, which is expat. The expat parser is a "push" parser (that is the same model as SAX), so the most efficient way to use it is with the SAX API. The XmlPullParser version has some more overhead from SAX since it needs to turn the underlying push semantics into a pull interface.

如果它可以帮助 - 推意味着它推解析的数据给你(你的回调实现给你每个标签和其他文档元素),而拉意味着您拨打解析器来检索每个元素

If it helps -- push means that it pushes the parsed data to you (callbacks you implement giving you each tag and other document element), while pull means you make calls to the parser to retrieve each element.

这篇关于XmlPullParser如何实现RES /生/ XML / xmlfilename?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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