Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func parseNews(url: String, completionHadler: (([RSSItem]) -> Void)?){
- self.parserCompletionHandler = completionHadler
- let request = URLRequest(url: URL(string: url)!)
- let urlSession = URLSession.shared
- let task = urlSession.dataTask(with: request) { (data, responce, error) in
- guard let data = data else {
- if let error = error {
- print(error.localizedDescription)
- }
- return
- }
- let parser = XMLParser(data: data)
- parser.delegate = self
- parser.parse()
- }
- task.resume()
- }
- func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
- currentItem = elementName
- if currentItem == "item" {
- currentItem = ""
- currentImage = ""
- currentDecription = ""
- }
- func parser(_ parser: XMLParser, foundCharacters string: String) {
- print(currentItem)
- switch currentItem {
- case "title": currentTitle += string
- case "description": currentDecription += string
- case "enclosure": currentImage += string
- default: break
- }
- func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
- if elementName == "item" {
- let rssItem = RSSItem(nameOfResourse: currentNameOfSourse, newsTitle: currentTitle, newsImage: currentImage, newsDesription: currentDecription)
- rssItems.append(rssItem)
- }
- }
- func parserDidEndDocument(_ parser: XMLParser) {
- parserCompletionHandler?(rssItems)
- }
- func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
- print(parseError.localizedDescription)
- }
- func parser(_ parser: XMLParser, foundCharacters string: String) {
- print(currentItem)
- switch currentItem {
- case "title": currentTitle += string
- case "description": currentDecription += string
- case "enclosure": currentImage += string
- default: break
- }
- rss
- channel
- language
- language
- title
- title
- title
- description
- description
- link
- link
- image
- url
- url
- title
- title
- link
- link
- width
- width
- height
- height
- height
- atom:link
- guid
- guid
- title
- title
- link
- link
- description
- description
- description
- description
- pubDate
- pubDate
- enclosure
- category
- category
- category
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement