【问题标题】:Java extracting every HTML content in a String listJava 提取字符串列表中的每个 HTML 内容
【发布时间】:2015-04-02 09:10:35
【问题描述】:

我实际上是在用 JAVA android 制作我的第一个 RSS 阅读器,但我遇到了一个问题。

其实我得到了一些 RSS 信息,但是周围都是 HTML 标签。

我需要的是提取这些标签中的每个 HTML content 并将它们放在一个字符串列表中,但我不知道该怎么做。

你能帮我解决这个问题吗?

感谢提前

【问题讨论】:

标签: java android


【解决方案1】:

如果你的 rss 是 xml 格式,你需要dom4j.jar

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class test {

    public static void main(String[] args) throws Exception {
        String rssUrl = ""; // paste url here
        List<RssDocument> docList = new ArrayList<RssDocument>();
        try
        {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(rssUrl);
            Element channel = (Element) document.getRootElement().element("channel");
            for (Iterator i = channel.elementIterator("item"); i.hasNext();)
            {
                Element element = (Element) i.next();
                String title = element.elementText("title");
                String pubDate = element.elementText("pubDate");
                String description = element.elementText("description");
                RssDocument doc = new RssDocument(title, pubDate, description);
                docList.add(doc);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // do something with docList
    }

    public static class RssDocument {
        String title;
        String pubDate;
        String description;

        RssDocument(String title, String pubDate, String description) {
            this.title = title;
            this.pubDate = pubDate;
            this.description = description;
        }
    }
}

将你的 rss url 粘贴到变量“rssUrl”中,然后运行这个 main。您将获得一个 RSS 文档列表,其中包含标题、发布日期和描述。


如果您只需要每个 rss 项目的标题和描述,请使用以下代码。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class test {

    public static void main(String[] args) throws Exception {
        String rssUrl = ""; // paste url here
        List<String> strList = new ArrayList<String>();
        try
        {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(rssUrl);
            Element channel = (Element) document.getRootElement().element("channel");
            for (Iterator i = channel.elementIterator("item"); i.hasNext();)
            {
                Element element = (Element) i.next();
                String title = element.elementText("title").replaceAll("\\<.*?>","");
                String description = element.elementText("description").replaceAll("\\<.*?>","");
                strList.add(title + " " + description);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

那么strList就是字符串列表,包含标题和描述。

例如:

{
 "title1 description1"
 "title2 description2"
 "title3 description3"
}

【讨论】:

    【解决方案2】:

    假设您有一个名为 htmlString 的 html 内容,您可以使用正则表达式对其进行清理。

    String htmlString = "<tr><td>12345</td></tr>";
    String noHTMLString = htmlString.replaceAll("\\<.*?>","");
    

    【讨论】:

    • 这很好用,但我需要一个字符串列表:-)。感谢您的帮助
    • 您的 html 数据流是作为单个字符串还是多个字符串接收?如果您可以从多个字符串的表单中获取输入 html,那么您可以循环应用@Mark Lee 的答案。类似于(String s: htmlSourceList) { // call a method based on @Mark Lee's solution here }
    【解决方案3】:

    这应该将 html 标记之间的所有内容的列表提取到称为匹配项的列表中。您应该修改括号中的正则表达式以匹配您的内容。当前版本仅匹配包含数字、字母、点、逗号、括号、减号和空格的文本。

    Pattern pattern = Pattern.compile("<\\w+>([\\w\\s\\.,\\-\\(\\)]+)</\\w+>");
    Matcher matcher = pattern.matcher(content);
    
    List<String> matches = new ArrayList<String>();
    while(matcher.find()){
        matches.add(matcher.group(1));
    }
    

    【讨论】:

    • 你有例子吗?
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2018-10-07
    相关资源
    最近更新 更多