在onCreate中永远不会到达XML Parser

时间:2014-05-15 19:31:14

标签: java android xml parsing

我已经根据此处的Android文档示例创建了一个XML解析器:

http://developer.android.com/training/basics/network-ops/xml.html

docs的实现和我自己的实现之间的主要区别是我正在尝试在我的Activity中实现XML解析器 - 但是当我尝试这样做时,代码似乎没有达到,尽管它在onCreate中(我虽然会启动它)我不确定应该做些什么来纠正这个问题 - 但是非常感谢任何建议/输入。

我已经尝试在我的解析器末尾移动结束括号但是我最终遇到了84个新错误(大多数是重复的局部变量错误 - 我可以在必要时提供它们)

我相信我需要将括号保留在原来的位置 - 并且只是以某种方式从android文档示例中调用解析器 - 但我不确定应该如何完成。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


        // ///////////////////////////////////////////////////////
        // Read and Parse XML Data from BootConfiguration
        // ///////////////////////////////////////////////////////

        public List parse(InputStream in) throws XmlPullParserException,
        IOException {
            try {
                XmlPullParser parser = Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in, Environment.getExternalStorageDirectory()
                        + "/BootConfiguration.xml");
                parser.nextTag();
                return readFeed(parser);
            } finally {
                in.close();
            }

        }

        private List readFeed(XmlPullParser parser) throws XmlPullParserException,
        IOException {
            List entries = new ArrayList();

            parser.require(XmlPullParser.START_TAG, ns, "feed");
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                // Starts by looking for the entry tag
                if (name.equals("BootConfiguration")) {
                    entries.add(readEntry(parser));
                } else {
                    skip(parser);
                }
            }
            return entries;
        }

        public static class Entry {
            public final String url;
            public final String user;
            public final String password;

            private Entry(String url, String user, String password) {
                this.url = url;
                this.user = user;
                this.password = password;
            }
        }

        // Parses the contents of an entry. If it encounters a title, summary, or
        // link tag, hands them off
        // to their respective "read" methods for processing. Otherwise, skips the
        // tag.
        private Entry readEntry(XmlPullParser parser)
                throws XmlPullParserException, IOException {
            parser.require(XmlPullParser.START_TAG, ns, "BootConfiguration");
            String url = null;
            String user = null;
            String password = null;
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                if (name.equals("ServiceUrl")) {
                    url = readUrl(parser);
                } else if (name.equals("ApiUser")) {
                    user = readUser(parser);
                } else if (name.equals("ApiPassword")) {
                    password = readPassword(parser);

                } else {
                    skip(parser);
                }
            }
            return new Entry(url, user, password);
        }

        // Processes title tags in the feed.
        private String readUrl(XmlPullParser parser) throws IOException,
        XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, ns, "ServiceUrl");
            String url = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "ServiceUrl");
            return url;
        }

        // Processes title tags in the feed.
        private String readUser(XmlPullParser parser) throws IOException,
        XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, ns, "ApiUser");
            String user = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "ApiUser");
            return user;
        }

        // Processes summary tags in the feed.
        private String readPassword(XmlPullParser parser) throws IOException,
        XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, ns, "ApiPassword");
            String password = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "ApiPassword");
            return password;
        }

        // For the tags title and summary, extracts their text values.
        private String readText(XmlPullParser parser) throws IOException,
        XmlPullParserException {
            String result = "";
            if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
            return result;
        }

        private void skip(XmlPullParser parser) throws XmlPullParserException,
        IOException {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                throw new IllegalStateException();
            }
            int depth = 1;
            while (depth != 0) {
                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
                }
            }

            // //////////////////////////////////////////////////////
            // End XML Parser
            // //////////////////////////////////////////////////////

0 个答案:

没有答案