首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何进入必需的标签。XML

如何进入必需的标签。XML
EN

Stack Overflow用户
提问于 2019-05-23 05:24:23
回答 2查看 40关注 0票数 0

我有以下xml文件:

代码语言:javascript
复制
    <rss xmlns:mo="http://news.yandex.ru" version="2.0">
    <channel>
        <title>..</title>
        <link>.</link>
        <description>..</description>
        <lastBuildDate>..</lastBuildDate>
        <generator>..</generator>
        <image>
            <url>..</url>
            <title>..</title>
            <link>..</link>
        </image>
        <item>
            <title></title>
            <link></link>
            <description></description>
            <mo:full-text></mo:full-text>
            <author></author>
            <pubDate></pubDate>
        </item>
        <item>
         ....
        </item>
      Another item...till the end
     </channel>
   </rss>

我得去拿一份物品清单。

代码语言:javascript
复制
List<Item> items = new ArrayList<>();

类项目具有下一个字段:

代码语言:javascript
复制
public static class Item{

        public final String titleNews;
        public final String link;
        public final String description;
        public final String date;
        public final String author;

        private Item(String titleNews, String link, String description, String date, String author) {
            this.titleNews = titleNews;
            this.link = link;
            this.description = description;
            this.date = date;
            this.author = author;
        }
    }

我用https://developer.android.com/training/basics/network-ops/xml写的代码

问题是我不知道如何进入频道的标签,然后跳过无用的标签,直到项目的标签。然后将每一项添加到我的列表中。

代码语言:javascript
复制
private List<Item> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Item> items = new ArrayList<>();
    parser.require(XmlPullParser.START_TAG, ns, "rss");
    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("item")) {
            items.add(readItem(parser));
        } else {
            skip(parser);
        }
    }
    return items;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-23 05:36:52

您已经在这样做了,但是您需要一个类似的方法来处理channel内容。

当然,您可能应该有一个Channel对象来收集所有相关的频道信息,而不仅仅是Item对象的组合列表。

代码语言:javascript
复制
private List<Item> readRss(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Item> items = new ArrayList<>();
    parser.require(XmlPullParser.START_TAG, ns, "rss");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("channel")) {
            items.addAll(readChannel(parser));
        } else {
            skip(parser);
        }
    }
    return items;
}
代码语言:javascript
复制
private List<Item> readChannel(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Item> items = new ArrayList<>();
    parser.require(XmlPullParser.START_TAG, ns, "channel");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("item")) {
            items.add(readItem(parser));
        } else {
            skip(parser);
        }
    }
    return items;
}
代码语言:javascript
复制
private Item readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
    // code here
}
票数 2
EN

Stack Overflow用户

发布于 2019-05-23 05:38:17

我会使用xpath。https://www.baeldung.com/java-xpath

代码语言:javascript
复制
<?xml version="1.0"?>
  <Tutorials>
    <Tutorial tutId="01" type="java">
        <title>Guava</title>
        <description>Introduction to Guava</description>
        <date>04/04/2016</date>
        <author>GuavaAuthor</author>
    </Tutorial>
    <Tutorial tutId="02" type="java">
      <title>XML</title>
      <description>Introduction to XPath</description>
      <date>04/05/2016</date>
      <author>XMLAuthor</author>
    </Tutorial>
  </Tutorials>

^ XML示例文件

vvvv Xpath代码。

代码语言:javascript
复制
FileInputStream fileIS = new FileInputStream(this.getFile());
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/Tutorials/Tutorial";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56265217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档