如何解析在迅速
整个RSS提要的XML数据我迅速的编程是新.. 我想在迅速解析XML数据..如何解析在迅速
这是我的XML数据..
<rss version="2.0">
<channel>
<title>Zee News :India National</title>
<link>
<![CDATA[ http://zeenews.india.com ]]>
</link>
<description>
<![CDATA[
Visit Zee News for latest India news, get latest news from India and all over the world. We provide you latest celebrity news, latest bollywood news and entertainment news with fastest top stories online about cricket, sports, business, bollywood, entertainment, lifestyle, world and science to get yourself updated.
]]>
</description>
<language>en-us</language>
<copyright>Zee News Ltd</copyright>
<category>India National News</category>
<pubDate>
<![CDATA[ 2/04/2016 11:16:57 AM GMT ]]>
</pubDate>
<image>
<url>
<![CDATA[
http://znn.india.com/images/logo-zeenews-aab-nw.jpg
]]>
</url>
<title>
<![CDATA[ Zee News ]]>
</title>
<link>
<![CDATA[ http://zeenews.india.com ]]>
</link>
</image>
<item>
<title>
Son of retired Major General arrested in Goa for suspected `terror links`
</title>
<link>
<![CDATA[
http://zeenews.india.com/news/india/son-of-retired-major-general-arrested-in-goa-for-suspected-terror-links_1852160.html
]]>
</link>
<description>
Samir Sardana was picked up from Vasco railway station after he was found "wandering suspiciously".
</description>
<comments>mailto:[email protected]</comments>
<pubdate>Thursday, February 04, 2016, 10:13 GMT +5:30</pubdate>
<author/>
<guid isPermaLink="false">
<![CDATA[
http://zeenews.india.com/news/india/son-of-retired-major-general-arrested-in-goa-for-suspected-terror-links_1852160.html
]]>
</guid>
</item>
我试图解析,但只有“项”数据可以解析不是“标题” &“图像”
这里是我的代码...
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var title3 = NSMutableString()
var date = NSMutableString()
var link = NSMutableString()
func beginParsing()
{
posts = []
parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://zeenews.india.com/rss/india-national-news.xml"))!)!
parser.delegate = self
parser.parse()
tbData!.reloadData()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?,attributes attributeDict:[NSObject :AnyObject])
{
if (elementName as NSString).isEqualToString("title")
{
elements = NSMutableDictionary.alloc()
title3 = NSMutableString()
title3 = ""
}
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary.alloc()
elements = [:]
title1 = NSMutableString()
title1 = ""
date = NSMutableString()
date = ""
link = NSMutableString()
link = ""
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("title")
{
if !title3.isEqual(nil) {
elements.setObject(title3, forKey: "title")
//posts.addObject(elements)
}
}
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "date")
}
if !link.isEqual(nil){
elements.setObject(link, forKey: "link")
}
posts.addObject(elements)
}
//posts.addObject(elements)
//posts.addObject(post)
}
func parser(parser: NSXMLParser, foundCharacters string: String?)
{
if element.isEqualToString("title")
{
title3.appendString(string!)
}
if element.isEqualToString("title") {
title1.appendString(string!)
}
if element.isEqualToString("link"){
link.appendString(string!)
}
if element.isEqualToString("pubDate") {
date.appendString(string!)
}
}
Plz ...建议任何解决方案如何我可以解析整个rss feed数据...
我纯粹是猜测现在。
你说item
解析得很好,而title
和image
没有。
望着XML似乎image
例如只包含CDATA元素
<image>
<url>
<![CDATA[http://znn.india.com/images/logo-zeenews-aab-nw.jpg]]>
</url>
<title>
<![CDATA[ Zee News ]]>
</title>
<link>
<![CDATA[ http://zeenews.india.com ]]>
</link>
</image>
我发现这里this post Stack Overflow上如果有人询问如何在XML处理CDATA。事实证明,NSXMLParserDelegate
有一个名为可选方法:
parser(_ parser: NSXMLParser, foundCDATA CDATABlock: NSData)
这里读到它或许可以帮助你吗?
谢谢你..为此。 ..我会尝试这...但我想解析标题也...当我试图解析标题和运行时,我的调试器检查“func解析器(解析器:NSXMLParser,foundCharacters字符串:字符串?)“,这是标题里面的通道和标题内的项目,然后错误会来....它不解析和存储在数组中.. – Ruhi
** NSXMLParser **和** NSXMLParserDelegate **有已被重命名为** XMLParser **&** XMLParserDelegate **。 –
可能的重复[如何使用Swift制作RSS阅读器](http://stackoverflow.com/questions/24468338/how-do-you-make-a-rss-reader-using-swift) – Abhijeet
There很少有解析XML数据的github项目。 1. [链接](https://github.com/wantedly/swift-rss-sample) 2. [链接](https://github.com/tibo/SwiftRSS) – Abhijeet