开发者

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. 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).

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.

So WITH EXAMPLES (I really need to learn this by 开发者_StackOverflowseeing 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); to get the resource

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

Use the XML Pull Parser to get elements from the resource

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜