在android中是否有标准的xml解析器,我可以为RSS添加任何标准链接,它会给我结果吗?我找到了一个例子,但我必须指定项目的路径。 有点像这样的链接 http://www.rssboard.org/files/sample-rss-2.xml
try {
URL conn = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
XmlParserHelper helper = new XmlParserHelper(conn.openStream(), "rss");
final String TITLES = "language";
XmlParseObject object = helper.createObject(TITLES, "//channel//item//title");
object.setParseAttributes();
object.setParseContent();
Map<String, List<XmlObject>> results = helper.parse();
List<XmlObject> titlesList = results.get(TITLES);
for (XmlObject title : titlesList) {
Log.d("Guid:", title.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
public class XmlParserHelper
{
protected String mRootName = "";
protected InputStream mXmlStream = null;
protected List<XmlParseObject> mParseList = new ArrayList<XmlParseObject>();
/**
* Initialize xml helper with file stream
* @param xmlStream input xml stream
*/
public XmlParserHelper(InputStream xmlStream, String rootName)
{
this.mXmlStream = xmlStream;
this.mRootName = rootName;
}
/**
* Point parse all attributes for XPath
* @param objectName key for attributes list in response
* @param XPath path to tag
*/
public void createAttributesObject(String objectName, String XPath)
{
XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
currentObject.setParseAttributes();
mParseList.add(currentObject);
}
public void createContentObject(String objectName, String XPath)
{
XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
currentObject.setParseContent();
mParseList.add(currentObject);
}
public XmlParseObject createObject(String objectName, String XPath)
{
XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
mParseList.add(currentObject);
return currentObject;
}
public Map<String, List<XmlObject>> parse() throws Exception
{
if (mRootName.equals(""))
{
throw new Exception("Root tag must be defined");
}
RootElement root = new RootElement(mRootName);
for (XmlParseObject parselable : mParseList)
{
parselable.configurateListenersForRoot(root);
}
try {
Xml.parse(mXmlStream, Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
Map<String, List<XmlObject>> result = new HashMap<String, List<XmlObject>>();
for (XmlParseObject parselable : mParseList)
{
result.put(parselable.getName(), parselable.getResults());
}
return result;
}
}
答案 0 :(得分:1)
对rss feed使用SAX解析器。另请参阅此链接以获取SAX解析示例。
http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/
请告诉我你的状况。
答案 1 :(得分:1)
URL url = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
//xpp.setInput(getInputStream(url), "UTF-8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
根据您的需要修改上述内容。我使用过XmlPullParser。