簡體   English   中英

在Android中使用jsoup在dom解析器中解析圖像

[英]parse image in dom parser using jsoup in android

我正在嘗試獲取該網站的RSS提要:

http://www.phonearena.com/feed

這是我的domparser活動:

public class DOMParser {
private RSSFeed _feed = new RSSFeed();

public RSSFeed parseXml(String xml) {

    URL url = null;
    try {
        url = new URL(xml);
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    try {

        DocumentBuilderFactory dbf;
        dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();


        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nl = doc.getElementsByTagName("item");
        NodeList itemChildren = null;
        Node currentItem = null;
        Node currentChild = null;
        int length = nl.getLength();

        for (int i = 0; i < length; i++) {
             currentItem = nl.item(i);
            RSSItem _item = new RSSItem();

            NodeList nchild = currentItem.getChildNodes();
            int clength = nchild.getLength();


            for (int j = 0; j < clength; j++) {

                currentChild = nchild.item(j);
                String theString = null;
                String nodeName = currentChild.getNodeName();

                theString = nchild.item(j).getFirstChild().getNodeValue();

                if (theString != null) {
                    if ("title".equals(nodeName)) {

                        _item.setTitle(theString);
                    }

                    else if ("description".equals(nodeName)) {

                        _item.setDescription(theString);

                        // Parse the html description to get the image url
                        String html = theString;
                        org.jsoup.nodes.Document docHtml = Jsoup
                                .parse(html);
                        Elements imgEle = docHtml.select("img");
                        _item.setImage(imgEle.attr("src"));
                    }

                    else if ("pubDate".equals(nodeName)) {


                        String formatedDate = theString.replace(" +0000",
                                "");
                        _item.setDate(formatedDate);
                    }

                }
            }


            _feed.addItem(_item);
        }

    } catch (Exception e) {
    }


    return _feed;
}
}     

除了我試圖通過jsoup獲得的圖像以外,其他所有東西都工作正常。

有人可以告訴我我做錯了什么還是想念什么?

在將變量theString傳遞給Jsoup之前,需要對其進行轉義。

else if ("description".equals(nodeName)) {
    _item.setDescription(theString);

    // Unescape then Parse the html description to get the image url
    Element imgEle = Jsoup.parse( //
            Parser.unescapeEntities( //
                  Parser.xmlParser().parseInput(theString, "").outerHtml(), //
                  true //
            )) //
            .select("img").first();

    if (imgEle != null) {
        _item.setImage(imgEle.attr("src"));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM