如何调整我的DOMParser来解析"媒体:内容"来自RSS on Java(Android)?

时间:2016-12-01 07:15:11

标签: java android xml rss domparser

首先,我有这个DOMParser类;

import android.util.Log;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DOMParser {

    private RSSFeed _feed = new RSSFeed();

    public RSSFeed parseXml(String xml) {
        // _feed.clearList();
        URL url = null;
        try {
            url = new URL(xml);
            Log.e("THE XML", xml);
            Log.e("THE URL", url.toString());
        } catch (MalformedURLException e1) {
            Log.e("MALFORMED EXCEPTION", "1");
            e1.printStackTrace();
        }

        try {
            // Create required instances
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new EntityResolver() {
                @Override
                public InputSource resolveEntity(String arg0, String arg1)
                        throws SAXException, IOException {
                    if (arg0.contains("Hibernate")) {
                        return new InputSource(new StringReader(""));
                    } else {
                        // TODO Auto-generated method stub
                        return null;
                    }
                }
            });
            // Parse the xml
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            // Get all <item> tags.
            NodeList nl = doc.getElementsByTagName("item");
            int length = nl.getLength();

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

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

                // Get the required elements from each Item
                for (int j = 0; j < clength; j = j + 1) {
                    try {
                        Node thisNode = nchild.item(j);
                        String theString = null;
                        String nodeName = thisNode.getNodeName();
                        Log.e("NODE NAME", nodeName);
                        theString = nchild.item(j).getFirstChild().getNodeValue();
                        //Log.e("THE STRING", theString);
                        if (theString != null) {
                            if ("title".equals(nodeName)) {
                                // Node name is equals to 'title' so set the Node
                                // value to the Title in the RSSItem.
                                _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)) {

                                // We replace the plus and zero's in the date with
                                // empty string
                                String formatedDate = theString.replace(" +0000",
                                        "");
                                _item.setDate(formatedDate);
                            } else if ("link".equals(nodeName)) {

                                // Trying to get the URL as a string
                                _item.setURL(theString);
                            }
                        /*else if ("media:content".equals(nodeName)){
                            _item.setImage(theString);
                            Log.e("THE IMAGE LINK", theString);
                        }*/

                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                // add item to the list
                _feed.addItem(_item);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // Return the final feed once all the Items are added to the RSSFeed
        // Object(_feed).
        return _feed;
    }

}  
    }

我正在尝试解析看起来像这样的条目;

<item>
  <title><![CDATA[Oceans Full of 'Aliens' Could Be Hidden Beneath Earth's Surface, Expert Says]]></title>
  <description><![CDATA[Do "aliens" exist on Earth? In a way, experts think so, and they believe that these creatures can be found thriving in massive underground oceans hidden hundreds of miles beneath the Earth's surface.]]></description>
  <guid>http://www.natureworldnews.com/articles/33160/20161130/oceans-full-aliens-hidden-beneath-earths-surface-expert.htm</guid>
  <link>http://www.natureworldnews.com/articles/33160/20161130/oceans-full-aliens-hidden-beneath-earths-surface-expert.htm</link>
  <media:content url="http://images.natureworldnews.com/data/images/full/37450/earth-ocean.jpg" />
  <media:title type="html"><![CDATA[earth ocean]]></media:title>
  <media:text type="html"><![CDATA[Do "aliens" exist on Earth? In a way, experts think so, and they believe that these creatures can be found thriving in massive underground oceans hidden hundreds of miles beneath the Earth's surface.]]></media:text>
  <category>
      <name><![CDATA[News]]></name>
  </category>
    <pubDate>Wed, 30 Nov 2016 11:02:00 EST</pubDate>
</item>
<item>
  <title><![CDATA[Great Barrier Reef Sees Its Worst Damage on Record]]></title>
  <description><![CDATA[The Great Barrier Reef is reportedly experiencing its worst damage via coral bleaching by far in history. The culprit is none other than the significant increase in water temperatures, which is record high as well. More than half of the coral population in the northern section has perished, while the central and southern centers have been reported to be in better health.]]></description>
  <guid>http://www.natureworldnews.com/articles/33132/20161130/great-barrier-reef-sees-worst-damage-record.htm</guid>
  <link>http://www.natureworldnews.com/articles/33132/20161130/great-barrier-reef-sees-worst-damage-record.htm</link>
  <media:content url="http://images.natureworldnews.com/data/images/full/37433/great-barrier-reef-sees-its-worst-damage-on-record.jpg" />
  <media:title type="html"><![CDATA[Great Barrier Reef Sees Its Worst Damage on Record]]></media:title>
  <media:text type="html"><![CDATA[Corals in the Great Barrier reef are in danger.
]]></media:text>
  <category>
      <name><![CDATA[News]]></name>
  </category>
    <pubDate>Wed, 30 Nov 2016 09:54:00 EST</pubDate>
</item>

请注意<media:content>标记 - 这是图片的网址所在的位置。

我的代码为每个RSS条目抛出以下内容!有人可以解释我在下面看到的#text值吗?有人可以帮我编码如何提取图片网址并将其放在setImage方法中吗?

12-01 01:58:36.278 27776-27823/com.example01 E/NODE NAME: media:content
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at com.climatenews07.parser.DOMParser.parseXml(DOMParser.java:74)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:103)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:97)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err:     at java.lang.Thread.run(Thread.java:761)
12-01 01:58:36.278 27776-27823/com.example01 E/NODE NAME: #text
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at com.climatenews07.parser.DOMParser.parseXml(DOMParser.java:74)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:103)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:97)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err:     at java.lang.Thread.run(Thread.java:761)
12-01 01:58:36.279 27776-27823/com.example01 E/NODE NAME: media:title

因此,我也得到以下异常;

12-01 01:58:36.500 27776-27927/com.example01 E/Image URL: http:
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: java.net.UnknownHostException: Invalid host: http:
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.HttpUrl.getChecked(HttpUrl.java:670)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.OkHttpClient$1.getHttpUrlChecked(OkHttpClient.java:165)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:345)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:331)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:398)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.climatenews07.image.ImageLoader.getBitmap(ImageLoader.java:74)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.climatenews07.image.ImageLoader.access$000(ImageLoader.java:27)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at com.climatenews07.image.ImageLoader$PhotosLoader.run(ImageLoader.java:148)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.501 27776-27927/com.example01 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.501 27776-27927/com.example01 W/System.err:     at java.lang.Thread.run(Thread.java:761)

1 个答案:

答案 0 :(得分:1)

  

有人可以帮我编码如何提取图片网址

提取url元素的media:content属性的值:

import org.w3c.dom.Element;
…
if ("media:content".equals(nodeName)) {
    Element contentElement = (Element) thisNode;
    if (contentElement.hasAttribute("url")) {
        String u = contentElement.getAttribute("url");
    }
}

该代码段将Node thisNode投射到Element,以便getAttribute(…)方法可用于获取url属性的值。

  

我的代码为每个RSS条目抛出以下内容!

问题中的代码是这样做的:

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

...当nchild.item(j)时,例如,这个:

<media:content url="http://images.natureworldnews.com/data/images/full/37450/earth-ocean.jpg" />

因此,在这种情况下,代码在没有子节点的.getFirstChild()元素上调用media:content,因此返回null。然后代码在其上调用.getNodeValue(),这会导致java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference错误。

代码的目的似乎是获取url属性的值。但属性不是孩子,因此.getFirstChild()不会获得url属性。应该使用.getAttribute(…)代替。

  

有人可以解释我在下面看到的#text值

每个item元素不仅包含子元素,还包含文本节点 - 因为元素之间的空间。 .getChildNodes()返回文本节点以及元素节点。

跳过文本节点的一种方法是,在for循环的代码中添加如下内容:

if ("#text".equals(nodeName)) {
    continue;
}
相关问题