如何使用XmlPullParser Android读取没有StartTag或Endtag的XML标签

时间:2018-09-20 23:59:45

标签: android xml xml-parsing xmlpullparser

这是我正在尝试使用XmlPullParser在Android Java中读取的XML,但是由于Vue.config.productionTip = false new Vue({ el: "#app", data: { showMenu: false, items: ['Option 1', 'Option 2', 'Option 3', 'Option 4'] }, computed: { listClass: function() { if (this.showMenu) { return 'show'; } return ''; } }, methods: { toggle: function() { this.showMenu = !this.showMenu this.showMenu && this.$nextTick(() => { document.addEventListener('click', this.hide) }) }, hide: function() { this.showMenu = false document.removeEventListener('click', this.hide) } } })标签是自封闭标签,因此我找不到方法

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

ul {
  list-style: none;
  display: none;
}

li {
  padding: 5px;
  border: 1px solid #000;
}

li:hover {
  cursor: pointer;
  background: #aaa;
}

.show {
  display: block;
}

这是我的Java代码段,试图通过xml归档阅读内容

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
  <h3>
    This is one demo:
  </h3>
  <button @click.stop="toggle" tabindex="0">
    Toogle menu
  </button>
  <ul :class="listClass" @click.stop>
    <li v-for="(item, key) in items" tabindex="0">{{ item }}</li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:0)

我终于可以使用以下代码读取自封闭标签:

protected Exception doInBackground(Integer... integers) {
            try {

                url = new URL(RSS_URL_2);

                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);

                XmlPullParser xpp = factory.newPullParser();

                xpp.setInput(getInputStream(url), "UTF_8");

                boolean insideItem = false;

                int eventType = xpp.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if (xpp.getName().equalsIgnoreCase("entry")) {
                            insideItem = true;
                        } else if (xpp.getName().equalsIgnoreCase("title")) {
                            if (insideItem) {
                                titles.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equals("link")) {
                            if (insideItem) {
                                links.add(xpp.getAttributeValue(null, "href"));
                            }
                        } else if (xpp.getName().equalsIgnoreCase("content")) {
                            if (insideItem) {
                                content.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("published")) {
                            if (insideItem) {
                                published.add(xpp.nextText());
                            }
                        }
                    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("entry")) {
                        insideItem = false;
                    }

                    eventType = xpp.next();
                }

            } catch (MalformedURLException me) {
                exception = me;
            } catch (XmlPullParserException xp) {
                exception = xp;
            } catch (IOException ie) {
                exception = ie;
            }

            return exception;
        }