1

我是 Android 新手,想读取 XML 文件并将内容写入ListView

XML Parser 一定是问题所在。我希望这只是我的代码中的一个小错误。

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>

解析器

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.setAdapter(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;
        }
}
4

2 回答 2

1

我想,你可以访问这个网站http://www.ibm.com/developerworks/opensource/library/x-android/。阅读 Easier SAX parsing 部分,我认为那里提出的解决方案会比你所知道的更好,绝对不容易出错。

于 2011-07-02T21:11:30.170 回答
0

我的工作代码:

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

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

            }
于 2011-07-03T14:39:12.810 回答