【问题标题】:Parse <enclosure> tag using Java使用 Java 解析 <enclosure> 标记
【发布时间】:2017-11-27 04:04:37
【问题描述】:

我想解析这行 rss 提要

&lt;enclosure url="http://media.nu.nl/m/32zx6jhahx6v_sqr256.jpg/verdachte-aanslag-moslims-in-londen-vervolgd-terroristische-moord.jpg" length="0" type="image/jpeg"&gt;&lt;/enclosure&gt;

所以我可以在我的 ImageView 中使用它。

我已经编写了一个完整的类来解析 & 标签,但我不能以某种方式使用这个类,因为附件标签没有关闭,而是继续

这是我写的课

public class RSSReader {
    //Lists to store headlines, descriptions & images
    String url = "http://www.nu.nl/rss/Algemeen";
    List<String> titleList;
    List<String> descriptionList;
    List<String> imageList;
    public RSSReader() throws IOException{
        titleList = readRSS(url, "<title>");
        descriptionList= listFilter(readRSS(url, "<description>"), "&amp;nbsp;", "");
        imageList = readRSS(url, "<enclosure>");
    }
    public List<String> readRSS(String feedUrl, String tag) throws IOException, MalformedURLException {
        URL url = new URL(feedUrl);
        BufferedReader reader= new BufferedReader(new InputStreamReader(url.openStream()));
        String closingTag = new StringBuilder(tag).insert(1, "/").toString();
        String currentLine;
        List<String> tempList = new ArrayList<String>();
        while((currentLine = reader.readLine()) != null){
            Integer tagEndIndex = 0;
            Integer tagStartIndex = 0;
            while (tagStartIndex >= 0){
                tagStartIndex = currentLine.indexOf(tag, tagEndIndex);
                if(tagStartIndex >= 0){
                    tagEndIndex = currentLine.indexOf(closingTag, tagStartIndex);
                    tempList.add(currentLine.substring(tagStartIndex + tag.length(), tagEndIndex) + "\n");
                }
            }
        }
        tempList.remove(0);
        return tempList;
    }

    public List<String> getDesciptionList(){
        return descriptionList;
    }

    public List<String> getTitleList(){
        return titleList;
    }
    public List<String> getImageList(){
        return imageList;
    }

    public List<String> listFilter(List<String> tempList, String require, String replace){
        //Creates new List
        List<String> newList = new ArrayList<>();
        //Loops through old list and checks for the 'require' variable
        for(int i = 0; i < tempList.size(); i++){
            if(tempList.get(i).contains(require)){
                newList.add(tempList.get(i).replace(require, replace));
            }
            else{
                newList.add(tempList.get(i));
            }
        }
        return newList;
    }
}

谁能帮帮我?

【问题讨论】:

    标签: java android xml parsing


    【解决方案1】:

    解决我自己的问题。见 imagelist + 构造函数

    public class RSSReader {
        //Lists to store headlines, descriptions & images
        String url = "http://www.nu.nl/rss/Algemeen";
        List<String> titleList;
        List<String> descriptionList;
        List<String> imageList;
        public RSSReader() throws IOException{
            titleList = readRSS(url, "<title>", "</title>");
            descriptionList= listFilter(readRSS(url, "<description>", "</description>"), "&amp;nbsp;", "");
            imageList = readRSS(url, "<enclosure url \"", "\" length=\"0\" type=\"image/jpeg\"</enclosure>");
        }
        public List<String> readRSS(String feedUrl, String openTag, String closeTag) throws IOException, MalformedURLException {
            URL url = new URL(feedUrl);
            BufferedReader reader= new BufferedReader(new InputStreamReader(url.openStream()));
    
            String currentLine;
            List<String> tempList = new ArrayList<String>();
            while((currentLine = reader.readLine()) != null){
                Integer tagEndIndex = 0;
                Integer tagStartIndex = 0;
                while (tagStartIndex >= 0){
                    tagStartIndex = currentLine.indexOf(openTag, tagEndIndex);
                    if(tagStartIndex >= 0){
                        tagEndIndex = currentLine.indexOf(closeTag, tagStartIndex);
                        tempList.add(currentLine.substring(tagStartIndex + openTag.length(), tagEndIndex) + "\n");
                    }
                }
            }
            tempList.remove(0);
            return tempList;
        }
    
        public List<String> getDesciptionList(){
            return descriptionList;
        }
    
        public List<String> getTitleList(){
            return titleList;
        }
        public List<String> getImageList(){
            return imageList;
        }
    
        public List<String> listFilter(List<String> tempList, String require, String replace){
            //Creates new List
            List<String> newList = new ArrayList<>();
            //Loops through old list and checks for the 'require' variable
            for(int i = 0; i < tempList.size(); i++){
                if(tempList.get(i).contains(require)){
                    newList.add(tempList.get(i).replace(require, replace));
                }
                else{
                    newList.add(tempList.get(i));
                }
            }
            return newList;
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2018-05-19
      相关资源
      最近更新 更多