0

我想从此网址读取图像。

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

  <channel>

    <title>podCast411</title>
    <description>The 411 on podCasts, podCasters and podcasting</description>
    <link>http://www.podcast411.com</link>
     ..
     ..
    <itunes:explicit>No</itunes:explicit>

    <itunes:image href="http://www.podcast411.com/img/411_itunes.jpg"/>
     ..
  </channel>
</rss>

我可以使用以下代码从该提要中读取标题、描述。

Node results = doc.getDocumentElement();
            Element docElement = doc.getDocumentElement();
            NodeList childNodes = docElement.getChildNodes();
            NodeList itemLinks = docElement.getElementsByTagName("item");
            Log.d("TAG",
                    "Response:" + results.getLastChild() + " "
                            + childNodes.getLength() + "  " + itemLinks.getLength());


            for (int i = 0; i < itemLinks.getLength(); i++) {
                Element el = (Element) itemLinks.item(i);


                NodeList itemTitle = el.getElementsByTagName("title");
                Element itemFirst = (Element) itemTitle.item(0);
                String nTitle = itemFirst.getFirstChild().getNodeValue();


                NodeList itemdesc = el.getElementsByTagName("description");
                Element itemFirstdesc = (Element) itemdesc.item(0);
                String nDesc = stipHtml(itemFirstdesc.getFirstChild().getNodeValue());

}
public String stipHtml(String html) {
            return Html.fromHtml(html).toString();
        }

当我尝试使用相同的代码来读取 url 时,它不起作用。以下代码用于读取 imade url:

NodeList itemImage = el.getElementsByTagName("image");
                String imageUrl = null;
                for(i=0;i< itemImage.getLength();i++) {
                    Log.d("TAG", "Length of item is "+ itemImage.getLength());
                    Element itemFirstImage = (Element) itemImage.item(i);
                    NodeList url = itemFirstImage.getElementsByTagName("url");
                    Element link = (Element) url.item(i);
                    imageUrl = stipHtml(link.getFirstChild().getNodeValue());
                }

我的代码有什么问题?

我的 logcat 是:它显示所有元素。

D/TAG     ( 1901): List Data is called
D/TAG     ( 1901): Temp is created.......
D/TAG     ( 1901): RSS Data: title  : Twelve-die-in-Himachal-road-accident description:Twelve people, all belonging to a village in Himachal Pradesh's Chamba district, were killed when a private vehicle skidded off the road and fell into a 700-metre-deep gorge on Sunday night, police officials said. http://feeds.hindustantimes.com/~r/HT-IndiaSectionPage-Topstories/~3/fUUMziwJedI/story01.htm null
D/TAG     ( 1901): List Data is called
D/TAG     ( 1901): Temp is created.......
D/TAG     ( 1901): RSS Data: title  : Twelve-die-in-Himachal-road-accident description:Twelve people, all belonging to a village in Himachal Pradesh's Chamba district, were killed when a private vehicle skidded off the road and fell into a 700-metre-deep gorge on Sunday night, police officials said. http://feeds.hindustantimes.com/~r/HT-IndiaSectionPage-Topstories/~3/fUUMziwJedI/story01.htm null
D/TAG     ( 1901): List Data is called
D/TAG     ( 1901): Temp is created.......
D/TAG     ( 1901): RSS Data: title  : Twelve-die-in-Himachal-road-accident description:Twelve people, all belonging to a village in Himachal Pradesh's Chamba district, were killed when a private vehicle skidded off the road and fell into a 700-metre-deep gorge on Sunday night, police officials said. http://feeds.hindustantimes.com/~r/HT-IndiaSectionPage-Topstories/~3/fUUMziwJedI/story01.htm null

最后,通过显示以下 logcat 强制关闭应用程序:

E/dalvikvm( 1901): Out of memory: Heap Size=24263KB, Allocated=23071KB, Bitmap Size=375KB
D/AndroidRuntime( 1901): Shutting down VM
W/dalvikvm( 1901): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
I/dalvikvm-heap( 1901): Clamp target GC heap from 24.311MB to 24.000MB
D/dalvikvm( 1901): GC_FOR_MALLOC freed 18016 objects / 939208 bytes in 141ms
E/AndroidRuntime( 1901): FATAL EXCEPTION: main
E/AndroidRuntime( 1901): java.lang.OutOfMemoryError
E/AndroidRuntime( 1901):    at android.text.SpannableStringBuilder.resizeFor(SpannableStringBuilder.java:137)
E/AndroidRuntime( 1901):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:322)
E/AndroidRuntime( 1901):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:275)
E/AndroidRuntime( 1901):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:438)
E/AndroidRuntime( 1901):    at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:259)
E/AndroidRuntime( 1901):    at android.text.HtmlToSpannedConverter.characters(Html.java:774)
E/AndroidRuntime( 1901):    at org.ccil.cowan.tagsoup.Parser.pcdata(Parser.java:994)
E/AndroidRuntime( 1901):    at org.ccil.cowan.tagsoup.HTMLScanner.save(HTMLScanner.java:614)
E/AndroidRuntime( 1901):    at org.ccil.cowan.tagsoup.HTMLScanner.scan(HTMLScanner.java:557)
E/AndroidRuntime( 1901):    at org.ccil.cowan.tagsoup.Parser.parse(Parser.java:449)
E/AndroidRuntime( 1901):    at android.text.HtmlToSpannedConverter.convert(Html.java:411)
E/AndroidRuntime( 1901):    at android.text.Html.fromHtml(Html.java:139)
E/AndroidRuntime( 1901):    at android.text.Html.fromHtml(Html.java:102)
E/AndroidRuntime( 1901):    at com.mistral.droidcast.SubscriptionActivity$RequestTask.stipHtml(SubscriptionActivity.java:134)
E/AndroidRuntime( 1901):    at com.mistral.droidcast.SubscriptionActivity$RequestTask.onPostExecute(SubscriptionActivity.java:84)
E/AndroidRuntime( 1901):    at com.mistral.droidcast.SubscriptionActivity$RequestTask.onPostExecute(SubscriptionActivity.java:1)
E/AndroidRuntime( 1901):    at android.os.AsyncTask.finish(AsyncTask.java:417)
E/AndroidRuntime( 1901):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
E/AndroidRuntime( 1901):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
E/AndroidRuntime( 1901):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1901):    at android.os.Looper.loop(Looper.java:144)
E/AndroidRuntime( 1901):    at android.app.ActivityThread.main(ActivityThread.java:4937)
E/AndroidRuntime( 1901):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1901):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1901):    at 

    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    E/AndroidRuntime( 1901):    at 

    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    E/AndroidRuntime( 1901):    at dalvik.system.NativeStart.main(Native Method)
    W/ActivityManager(  103):   Force finishing activity com.mistral.droidcast/.SubscriptionActivity

请给我任何其他方式来实现它。

4

1 回答 1

0
try {
            InputStream myxml = this.getApplicationContext().getAssets().open("production.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(myxml);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("channel");
            name = new TextView[nodeList.getLength()];

            for (int i = 0; i<nodeList.getLength(); i++) 
            {
                Node node = nodeList.item(i);               
                Element fstElmnt = (Element) node;
                NodeList toList = fstElmnt.getElementsByTagName("itunes:image");
                Element nameElement = (Element) toList.item(0);
                toList = nameElement.getChildNodes();     
                String link = ((Node) toList.item(0)).getNodeValue();
                 // link would be containing url of ur image        
            }


            } catch (Exception e) {
                    System.out.println("XML Pasing Excpetion = " + e);
            }
于 2013-04-15T07:29:30.270 回答