我有一個xml解析器,用於解析sharontalon.com/feed的提要 但是,當將字符串放入表中時,提要的描述將用作第一個項目描述,碰撞所有的帖子描述都放在一個項目中。我怎樣才能跳過第一個描述標籤?XML Parser閱讀頻道描述標籤
http://i.stack.imgur.com/9kYVT.png
這是XMLParser的
import UIKit
@objc protocol XMLParserDelegate{
func parsingWasFinished()
}
class XMLParser: NSObject, NSXMLParserDelegate {
var arrParsedData = [Dictionary<String, String>]()
var currentDataDictionary = Dictionary<String, String>()
var currentElement = ""
var foundCharacters = ""
var delegate : XMLParserDelegate?
func startParsingWithContentsOfURL(rssURL: NSURL) {
let parser = NSXMLParser(contentsOfURL: rssURL)
parser!.delegate = self
parser!.parse()
}
func parserDidEndDocument(parser: NSXMLParser) {
delegate?.parsingWasFinished()
}
func parser(parser: NSXMLParser, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [String : String]) {
currentElement = elementName
}
func parser(parser: NSXMLParser, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
if !foundCharacters.isEmpty {
if elementName == "link" || elementName == "description"{
foundCharacters = (foundCharacters as NSString).substringFromIndex(3)
}
currentDataDictionary[currentElement] = foundCharacters
foundCharacters = ""
if currentElement == "pubDate" {
arrParsedData.append(currentDataDictionary)
}
}
}
func parser(parser: NSXMLParser, foundCharacters string: String!) {
if (currentElement == "title") || currentElement == "description" || currentElement == "link" || currentElement == "pubDate"{
foundCharacters += string
}
}
func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError!) {
print(parseError.description)
}
func parser(parser: NSXMLParser, validationErrorOccurred validationError: NSError!) {
print(validationError.description)
}
}
這是的TableView控制器
import UIKit
class TopicsTableViewController: UITableViewController, XMLParserDelegate {
var xmlParser : XMLParser!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://sharontalon.com/feed")
xmlParser = XMLParser()
xmlParser.delegate = self
xmlParser.startParsingWithContentsOfURL(url!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: XMLParserDelegate method implementation
func parsingWasFinished() {
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return xmlParser.arrParsedData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("idCell", forIndexPath: indexPath)
let currentDictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>
cell.textLabel?.text = currentDictionary["title"]
cell.detailTextLabel?.text = currentDictionary["description"]
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let dictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>
let tutorialLink = dictionary["link"]
let publishDate = dictionary["pubDate"]
let tutorialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("idTutorialViewController") as! TutorialViewController
tutorialViewController.tutorialURL = NSURL(string: tutorialLink!)
tutorialViewController.publishDate = publishDate
showDetailViewController(tutorialViewController, sender: self)
}
}
感謝所有幫助
我不知道你的問題是什麼。我試過你的代碼,[這是結果](http://s30.postimg.org/3zxfwiddd/Screen_Shot_2015_10_25_at_01_06_36.png),請問你的問題是什麼? –
我現在看到問題了,標題和描述之間總是有一個轉變。讓我試着幫忙 –
我寫了一個答案:) –