0
这是我从中提取数据的RSS XML文件。我用SAX解析器完成了所有工作,所有工作都很好。但是我解析了整个RSS文件并获取了每个单元素数据。Rss解析问题(XML)
但我的问题是我得到整个文件元素的数据,但我只想要只在元素。
<rss version="2.0">
<channel>
<title>RSS Feed</title>
<link>http://www.xyz.com</link>
<description>Calendar RSS Feeds</description>
<language>en-us</language>
<ttl>60</ttl>
<item>
<title>
title 1
</title>
<description>description 1</description>
<link>
http://www.xyz.com
</link>
<guid isPermaLink="false">[email protected]://www.xyz.com</guid>
</item>
<item>
<title>
title 2
</title>
<description>description 2</description>
<link>
http://www.xyz.com
</link>
<guid isPermaLink="false">[email protected]://www.xyz.com</guid>
</item>
<item>
</channel>
</rss>
我已经尝试了所有的组合来设置此我会告诉我的代码在这里我提出的startElement和 的endElement方法: -
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
current = true;
if (localName.equals("channel"))
{
itemList = new ItemList();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
current = false;
if(localName.equals("title"))
{
itemList.setTitle(currentValue);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if(current)
{
currentValue = new String(ch, start, length);
current=false;
}
}
任何一个请建议我如何只能retrive的仅在元素下的元素。
我也试过,但这个给了我错误
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
current = true;
if (localName.equals("channel"))
{
if (localName.equals("item"))
{
itemList = new ItemList();
}
}
}
在此先感谢。
我已经尝试过这样的DocumentBuilder我得到这个结果,但在这我不知道如何实现StringBuffer的,因为我不在DocumentBuider中获取完整的数据。你可以建议我如何使用DocumentBuilder实现StringBuffer.Thanks ... – user755278 2012-04-26 10:17:04
k ...我已经构建了一个RSS阅读器。我将发布我用于解析的整个文件。只需检查它... – 2012-04-26 10:19:49