XmlPullParser如何获取res/raw/xml/xmlfilename?
我是编程新手,所以如果我在下面的段落中出现错误,请先纠正我:
Android 中主要使用三种 xml 解析器:Sax、Dom 和 XmlPullParser。最后一个选项,同时作为外部资源存在。是Android的“核心”,因此工作速度更快,但功能有限
这是我的问题我稍微修改了下面链接中提供的代码
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文件中,我可以通过以下方式调用解析器:
public xmlPParser myxpp;
...
myxpp = new xmlPParser("<foo>Hi five !!</foo>");
在最后一行:我希望能够要求解析器转到文件,而不是向其传递一个字符串。我该怎么做? 我不知道如何利用这篇文章 getResources().getXml() 是否假设我正在使用我现在不确定是否使用的 Android pullParser ?
I am new to programming, so to start with correct me if I am wrong in the paragragh below :
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
I did the following :
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;
}
}
In another java file, I can call the parser in the following way :
public xmlPParser myxpp;
...
myxpp = new xmlPParser("<foo>Hi five !!</foo>");
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlPullParser 并不是真正的解析器,它是一种解析器类型的接口,称为“拉”解析器。
函数 getResources().getXml() 返回用于“解析”XML 资源的 XmlPullParser 的实现。这不是一个真正的 XML 解析器 —— 事实上,原始 XML 文件在构建到您的应用程序之前已在构建时进行了解析,而这个“XML 解析器”所做的只是在您调用其 API 时返回预先消化的 XML 结构。这是 Android 上可用的最快的“XML”解析器(因为它并不真正解析任何内容),但要求将 XML 文档作为构建应用程序的一部分进行编译。
从 XmlPullParserFactory.newInstance() 获得的 XmlPullParser 的另一个实现不是“受限的”——该实现功能齐全,并且可以解析您提供给它的任何原始 XML 文档。
至少在某一时刻(不确定情况是否仍然如此),SAX 解析器和 XmlPullParserFactory.newInstance() 返回的解析器实际上构建在相同的底层实现上,即 expat。 expat 解析器是一个“推送”解析器(与 SAX 具有相同的模型),因此最有效的使用方法是使用 SAX API。 XmlPullParser 版本比 SAX 有更多开销,因为它需要将底层推送语义转换为拉取接口。
如果有帮助的话,push 意味着它将解析的数据推送给您(您实现的回调为您提供每个标签和其他文档元素),而 pull 意味着您调用解析器来检索每个元素。
XmlPullParser is not really a parser, it is an interface to a type of parser, called a "pull" parser.
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.
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.
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.