如何进入必需的标签。 XML格式

时间:2019-05-22 21:24:23

标签: java android xml xml-parsing

我有以下xml文件:

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

我必须得到一个物品清单。

List<Item> items = new ArrayList<>();

Class Item具有下一个字段:

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

编写了代码

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

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

2 个答案:

答案 0 :(得分:2)

已经采用相同的方法,但是您需要类似的方法来处理channel内容。

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

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;
}
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;
}
private Item readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
    // code here
}

答案 1 :(得分:2)

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

<?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代码。

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