开发者

Noob question about Android and XmlParser

开发者 https://www.devze.com 2023-03-17 08:42 出处:网络
I\'m new to Android want to read a XML-file and write the content into a ListView The XML Parser must be the problem. I hope it is only a little mistake in my code.

I'm new to Android want to read a XML-file and write the content into a ListView

The XML Parser must be the problem. I hope it is only a little mistake in my code.

orderxml2.xml

<?xml version="1.0"?>
<order>
   <item id="1">
      <title>BiTest1</title>
      <group>G1</group>
      <price>5.00</price>
      <description>Example</description>
   </item>
   <item id="2">
      <title>Test2</title>
      <group>g1</group>
      <price>2.00</price>
      <description>Example</description>
   </item>
</order>

The Parser

public class Order extends Activity 
{
    private String[] lv_arr = {};
/** Called when the activity is first created. */
@Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.order);

            try {
                ArrayList<String> orderxmlarray = PrepareListFromXml();
                lv_arr = (String[]) orderxmlarray.toArray(new String[0]);
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ListView lv = (ListView) findViewById(R.id.listViewOrder1);

            lv.setAdap开发者_JAVA技巧ter(new ArrayAdapter<String>(Order.this, android.R.layout.simple_list_item_1, lv_arr));
        }

        public ArrayList<String> PrepareListFromXml() throws XmlPullParserException, IOException
        {
            ArrayList<String> orderItems = new ArrayList<String>();
            XmlResourceParser orderxml = getResources().getXml(R.xml.orderxml2);

            orderxml.next();
            int eventType = orderxml.getEventType();

            while (eventType != XmlResourceParser.END_DOCUMENT) 
            {
                if (eventType == XmlResourceParser.START_DOCUMENT) {
                    orderItems.add(orderxml.getAttributeValue(null, "title"));                              
                }

                else if(eventType == XmlResourceParser.END_DOCUMENT) {
                    orderItems.add(orderxml.getAttributeValue(null, "title"));          
                }

                try {
                    eventType = orderxml.next();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return orderItems;
        }
}


I think, you can visit this site http://www.ibm.com/developerworks/opensource/library/x-android/ . Read section Easier SAX parsing, I think that the solution presented there would be better than what you have right know, definitely less error prone.


My working code:

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (orderParser.getName().equalsIgnoreCase("title")) {
                    orderItems.add(orderParser.nextText());
                }

            }
0

精彩评论

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