我有一个解析sharontalon.com/feed供稿的xml解析器 但是,当将字符串放入表格时,将使用Feed的描述作为第一个项目描述,将所有帖子描述碰到一个项目。如何跳过第一个描述标签?
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)
}
}
感谢您的帮助
答案 0 :(得分:0)
问题正在发生,因为在您的XML数据中,description
标记内有一个cannel
标记,当您到达arrParsedData
时,您正在将数据插入pubDate
字典} description
代码,你运气不好,因为channel
代码已包含description
代码,所以你感觉不舒服错误。但如果它没有,则会发生异常,告诉您没有名为description
的标签。我改变了你的状况
if currentElement == "pubDate"
到:
if currentElement == "description" && !isFirst
我添加了isFirst
变量,以避免description
标记下的第一个channel
标记。请注意我已添加的else
部分
if currentElement == "description" && !isFirst
{
arrParsedData.append(currentDataDictionary)
}else {
if currentElement == "description" && isFirst {
isFirst = false
}
}
这是新的XMLParserDelegate
。我还纠正了NSXMLParserDelegate
方法的一些签名
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?
var isFirst = true
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 == "description" && !isFirst
{
arrParsedData.append(currentDataDictionary)
}else {
if currentElement == "description" && isFirst {
isFirst = false
}
}
}
}
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)
}
}