关于 Android 和 XmlParser 的菜鸟问题

发布于 2024-11-18 16:52:26 字数 2861 浏览 5 评论 0原文

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

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

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

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.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;
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-11-25 16:52:26

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

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.

美胚控场 2024-11-25 16:52:26

我的工作代码:

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

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

            }

My working code:

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

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

            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文