2015-10-25 119 views
0

我有一個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) 
 

 
     
 

 
    } 
 

 
    
 

 

 
}

感謝所有幫助

+0

我不知道你的問題是什麼。我試過你的代碼,[這是結果](http://s30.postimg.org/3zxfwiddd/Screen_Shot_2015_10_25_at_01_06_36.png),請問你的問題是什麼? –

+0

我現在看到問題了,標題和描述之間總是有一個轉變。讓我試着幫忙 –

+0

我寫了一個答案:) –

回答

0

問題是在你的XML數據,因爲發生的事情,還有就是cannel標籤內description標籤和您的數據插入到arrParsedData字典到達pubDate標籤時,並自帶下一個description標記中,您很不幸,因爲channel標記已包含description標記,因此您沒有感受到錯誤。但如果沒有,就會發生例外情況,告訴您沒有標籤description。我改變了你的病情

if currentElement == "pubDate" 

到:

if currentElement == "description" && !isFirst 

我加入了isFirst變量,以避免第一description標記,是channel標籤下。請也看到,我已經添加

if currentElement == "description" && !isFirst 

     { 
      arrParsedData.append(currentDataDictionary) 
     }else { 
      if currentElement == "description" && isFirst { 
       isFirst = false 
      } 
     } 

這是結果的截圖else部分: enter image description here

這是新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) 

    } 

}