帮助将xml文件加载到listview中以便在android中进行解析

时间:2022-08-26 12:50:26

I need a bit more detailed help on how to take an xml file, in the res/xml folder, and "load it" into memory. Ultimately I want to build nested listviews which allow the user to browse through the xml file. 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).

我需要一些关于如何在res / xml文件夹中获取xml文件并将其“加载”到内存中的更详细的帮助。最终,我想构建嵌套的列表视图,允许用户浏览xml文件。不幸的是我对Android很新,到目前为止给我的一些建议有点过高(有人向我展示了关于这个主题的IBM论文的链接......我有点迷失了)。

The xml file is sizeable, and could possibly get bigger. It's basically a portable database. The test one I am using has 4200 lines of xml code.

xml文件很大,可能会变大。它基本上是一个便携式数据库。我正在使用的测试有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!!

所以有了示例(我真的需要通过看到我能完全掌握它来学习这一点),任何人都可以帮助我学习“加载文件”的最佳方法并将至少*节点扩展到列表视图中吗?我知道为了“浏览”文件会涉及更多的编程,但如果我至少可以开始学习它,那么它可能会帮助我自己研究它。谢谢!!

2 个解决方案

#1


0  

Use Resource.getXmL(R.xmlfile); to get the resource

使用Resource.getXmL(R.xmlfile);获得资源

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

Use the XML Pull Parser to get elements from the resource

使用XML Pull Parser从资源中获取元素

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");
     }
 }

#2


0  

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)不适用于参数(XmlResourceParser) - 无法对非静态方法getXml进行静态引用( int)从类型Resources - Resources无法解析

#1


0  

Use Resource.getXmL(R.xmlfile); to get the resource

使用Resource.getXmL(R.xmlfile);获得资源

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

Use the XML Pull Parser to get elements from the resource

使用XML Pull Parser从资源中获取元素

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");
     }
 }

#2


0  

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)不适用于参数(XmlResourceParser) - 无法对非静态方法getXml进行静态引用( int)从类型Resources - Resources无法解析