我怎么能用Picasso来解析(我使用DOM解析)来自URL的图像,但是它已经包含在内容中:编码然后在CDATA中,我已经解析过,但我可以得到它加载图片。
我的XML如下所示:
<item>
<desc>
txt txt txt txt <img src="htttp://mysite.com/images/img.png"> txt txt
<img src="htttp://mysite.com/images/img.png"> ...
</desc>
</item>
<item>
<desc>
txt txt txt txt <img src="htttp://mysite.com/images/img.png"> txt txt
<img src="htttp://mysite.com/images/img.png"><img src="htttp://mysite.com/images/img.png">
</desc>
</item>
我只需要用Picasso下载它。
这是我的DOMParser.java:
package net.biscani.parser;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class DOMParser {
// Create a new RSS feed
private RSSFeed feed = new RSSFeed();
public RSSFeed parseXML(String feedURL) {
// Create a new URL
URL url = null;
try {
// Find the new URL from the given URL
url = new URL(feedURL);
} catch (MalformedURLException e) {
// Throw an exception
e.printStackTrace();
}
try {
// Create a new DocumentBuilder
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Parse the XML
Document doc = builder.parse(new InputSource(url.openStream()));
// Normalize the data
doc.getDocumentElement().normalize();
// Get all <item> tags.
NodeList list = doc.getElementsByTagName("item");
// Get size of the list
int length = list.getLength();
// For all the items in the feed
for (int i = 0; i < length; i++) {
// Create a new node of the first item
Node currentNode = list.item(i);
// Create a new RSS item
RSSItem item = new RSSItem();
// Get the child nodes of the first item
NodeList nodeChild = currentNode.getChildNodes();
// Get size of the child list
int cLength = nodeChild.getLength();
// For all the children of a node
for (int j = 1; j < cLength; j = j + 2) {
// Get the name of the child
String nodeName = nodeChild.item(j).getNodeName(), nodeString = null;
// If there is at least one child element
if(nodeChild.item(j).getFirstChild() != null){
// Set the string to be the value of the node
nodeString = nodeChild.item(j).getFirstChild().getNodeValue();
}
// If the string isn't null
if (nodeString != null) {
// Set the appropriate value
if ("title".equals(nodeName)) {
item.setTitle(nodeString);
} else if ("content:encoded".equals(nodeName)) {
item.setDescription(nodeString);
} else if ("pubDate".equals(nodeName)) {
item.setDate(nodeString.replace(" +0000", ""));
} else if ("author".equals(nodeName) || "dc:creator".equals(nodeName)) {
item.setAuthor(nodeString);
} else if ("link".equals(nodeName)){
item.setURL(nodeString);
} else if ("image".equals(nodeName)){
item.setThumb(nodeString);
}
}
}
// Add the new item to the RSS feed
feed.addItem(item);
}
} catch (Exception e) {
e.printStackTrace();
}
// Return the feed
return feed;
}
}
还有我的ListAdapter.java:
package net.biscani;
import net.biscani.R;
import net.biscani.parser.RSSFeed;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
// Create a new LayoutInflater
private LayoutInflater layoutInflater;
// Create a new RSSFeed
private RSSFeed feed;
public ListAdapter(Context c, RSSFeed rssFeed) {
// Set the layout inflater
layoutInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Set the RSS feed
feed = rssFeed;
}
@Override
public int getCount() {
// Set the total list item count
return feed.getItemCount();
}
@Override
public Object getItem(int position) {
// Return the position
return position;
}
@Override
public long getItemId(int position) {
// Return the position
return position;
}
@Override
public View getView(int position, View listItem, ViewGroup parent) {
// If a list item is null
if (listItem == null) {
// Inflate a list item
listItem = layoutInflater.inflate(R.layout.feed_list_item, null);
}
// Set the views in the layout
((TextView)listItem.findViewById(R.id.title)).setText(feed.getItem(position).getTitle());
// Bit of formatting for adding the author
((TextView)listItem.findViewById(R.id.date)).setText(feed.getItem(position).getDate());
((TextView)listItem.findViewById(R.id.description)).setText(Html.fromHtml(feed.getItem(position).getDescription()));
((TextView)listItem.findViewById(R.id.author)).setText(feed.getItem(position).getAuthor());
// Return the new list item
return listItem;
}
}