简体   繁体   English

帮助将 xml 文件加载到列表视图中,以便在 android 中进行解析

[英]help loading xml file into a listview for parsing in android

I need a bit more detailed help on how to take an xml file, in the res/xml folder, and "load it" into memory.我需要有关如何在 res/xml 文件夹中获取 xml 文件并将其“加载”到 memory 的更详细的帮助。 Ultimately I want to build nested listviews which allow the user to browse through the xml file.最终,我想构建允许用户浏览 xml 文件的嵌套列表视图。 Unfortunately I'm pretty new to Android and some of the suggestions given to me so far have been a bit too high level (someone showed me a link to IBM's treatise on the subject... I got a bit lost).不幸的是,我对 Android 还很陌生,到目前为止给我的一些建议有点太高了(有人向我展示了 IBM 关于该主题的论文的链接......我有点迷失了)。

The xml file is sizeable, and could possibly get bigger. xml 文件相当大,并且可能会变得更大。 It's basically a portable database.它基本上是一个可移植的数据库。 The test one I am using has 4200 lines of xml code.我正在使用的测试有 4200 行 xml 代码。

So WITH EXAMPLES (I really need to learn this by seeing so I can grasp it fully), can anyone please help me learn the best way to "load the file" and inflate at least the top node into a listview?因此,通过示例(我真的需要通过查看来了解这一点,以便我可以完全掌握它),任何人都可以帮助我学习“加载文件”并将至少顶部节点膨胀到列表视图中的最佳方法吗? I know alot more programming will be involved in order to "browse" the file, but if I can at least get this beginning step learned it would probably help me research it on my own.我知道为了“浏览”文件将涉及更多的编程,但如果我至少可以了解这个开始步骤,它可能会帮助我自己研究它。 Thank you!!谢谢!!

Use Resource.getXmL(R.xmlfile);使用 Resource.getXmL(R.xmlfile); to get the resource获取资源

http://developer.android.com/reference/android/content/res/XmlResourceParser.html http://developer.android.com/reference/android/content/res/XmlResourceParser.html

Use the XML Pull Parser to get elements from the resource使用 XML 拉解析器从资源中获取元素

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

     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( Resource.getXml(R.your_xml_file_id );
         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());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
         System.out.println("End document");
     }
 }
xpp.setInput( Resource.getXml(R.your_xml_file_id );

Corrections: - An extra ")" is missing.更正: - 缺少一个额外的“)”。 -.xml should be added to R. -.xml 应添加到 R。

But, I can't avoid this error message: Multiple markers at this line - The method setInput(Reader) in the type XmlPullParser is not applicable for the arguments (XmlResourceParser) - Cannot make a static reference to the non-static method getXml(int) from the type Resources - Resources cannot be resolved但是,我无法避免此错误消息:此行的多个标记 - XmlPullParser 类型中的方法 setInput(Reader) 不适用于 arguments (XmlResourceParser) - 无法对非静态方法 getXml( int) 来自资源类型 - 资源无法解析

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

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