1

我想要的只是解析一个本地 XML 文件。当我将 XML 文件放在资源文件夹中时,代码可以正常工作。这是我的代码。

try {

        XmlResourceParser xpp = getResources().getXml(R.xml.catalog);
        xpp.next();
        int eventType = xpp.getEventType();
        int iter = 0;
        String elemtext = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {

                String elemName = xpp.getName();
                if (elemName.equals("catalog")) {
                    String journalAttr = xpp.getAttributeValue(null,
                            "journal");
                    String publisherAttr = xpp.getAttributeValue(null,
                            "publisher");
                    journal.setText(journalAttr);
                    publisher.setText(publisherAttr);
                }
                if (elemName.equals("article")) {
                    iter = iter + 1;
                }

                if (elemName.equals("edition")) {
                    elemtext = "edition";
                }
                if (elemName.equals("title")) {
                    elemtext = "title";
                }
                if (elemName.equals("author")) {
                    elemtext = "author";
                }
            }

            else if (eventType == XmlPullParser.TEXT) {
                if (iter == 1) {
                    if (elemtext.equals("edition")) {
                        edition1.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title1.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author1.setText(xpp.getText());
                    }
                }

                else if (iter == 2) {
                    if (elemtext.equals("edition")) {
                        edition2.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title2.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author2.setText(xpp.getText());
                    }

                }
            }
            eventType = xpp.next();
        }

    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }

现在我的意图是如果将相同的文件放在 sdcard 中,则使代码正常工作。这是我的努力。

try {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        int eventType = xpp.getEventType();
        int iter = 0;
        String elemtext = null;
        // get a reference to the file.
        File file = new File(Environment.getExternalStorageDirectory()
                + "/newdir/catalog.xml");
        FileInputStream fis = new FileInputStream(file);

        xpp.setInput(new InputStreamReader(fis));

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {

                String elemName = xpp.getName();
                if (elemName.equals("catalog")) {
                    String journalAttr = xpp.getAttributeValue(null,
                            "journal");
                    String publisherAttr = xpp.getAttributeValue(null,
                            "publisher");
                    journal.setText(journalAttr);
                    publisher.setText(publisherAttr);
                }
                if (elemName.equals("article")) {
                    iter = iter + 1;
                }

                if (elemName.equals("edition")) {
                    elemtext = "edition";
                }
                if (elemName.equals("title")) {
                    elemtext = "title";
                }
                if (elemName.equals("author")) {
                    elemtext = "author";
                }
            }

            else if (eventType == XmlPullParser.TEXT) {
                if (iter == 1) {
                    if (elemtext.equals("edition")) {
                        edition1.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title1.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author1.setText(xpp.getText());
                    }
                }

                else if (iter == 2) {
                    if (elemtext.equals("edition")) {
                        edition2.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title2.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author2.setText(xpp.getText());
                    }

                }
            }
            eventType = xpp.next();
        }

    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }

使用此代码时出现空指针异常。帮我解决这个问题。

4

2 回答 2

1

我认为你没有正确解决。检查我的建议:

File root = android.os.Environment.getExternalStorageDirectory();
String a = root.getAbsolutePath() + "/newdir/catalog.xml";
FileInputStream fis = new FileInputStream(a);
于 2012-07-10T06:31:44.360 回答
1

此示例与您的需要相同

于 2012-07-10T06:27:27.437 回答