Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. func parseNews(url: String, completionHadler: (([RSSItem]) -> Void)?){
  2. self.parserCompletionHandler = completionHadler
  3.  
  4. let request = URLRequest(url: URL(string: url)!)
  5. let urlSession = URLSession.shared
  6. let task = urlSession.dataTask(with: request) { (data, responce, error) in
  7. guard let data = data else {
  8. if let error = error {
  9. print(error.localizedDescription)
  10. }
  11. return
  12. }
  13.  
  14. let parser = XMLParser(data: data)
  15. parser.delegate = self
  16. parser.parse()
  17.  
  18. }
  19. task.resume()
  20. }
  21.  
  22. func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
  23. currentItem = elementName
  24. if currentItem == "item" {
  25. currentItem = ""
  26. currentImage = ""
  27. currentDecription = ""
  28.  
  29. }
  30.  
  31.  
  32. func parser(_ parser: XMLParser, foundCharacters string: String) {
  33. print(currentItem)
  34. switch currentItem {
  35. case "title": currentTitle += string
  36. case "description": currentDecription += string
  37. case "enclosure": currentImage += string
  38. default: break
  39.  
  40. }
  41.  
  42.  
  43. func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
  44. if elementName == "item" {
  45. let rssItem = RSSItem(nameOfResourse: currentNameOfSourse, newsTitle: currentTitle, newsImage: currentImage, newsDesription: currentDecription)
  46. rssItems.append(rssItem)
  47. }
  48. }
  49.  
  50. func parserDidEndDocument(_ parser: XMLParser) {
  51. parserCompletionHandler?(rssItems)
  52. }
  53.  
  54. func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
  55. print(parseError.localizedDescription)
  56. }
  57.  
  58. func parser(_ parser: XMLParser, foundCharacters string: String) {
  59. print(currentItem)
  60. switch currentItem {
  61. case "title": currentTitle += string
  62. case "description": currentDecription += string
  63. case "enclosure": currentImage += string
  64. default: break
  65.  
  66. }
  67.  
  68. rss
  69. channel
  70. language
  71. language
  72. title
  73. title
  74. title
  75. description
  76. description
  77. link
  78. link
  79. image
  80. url
  81. url
  82. title
  83. title
  84. link
  85. link
  86. width
  87. width
  88. height
  89. height
  90. height
  91. atom:link
  92.  
  93. guid
  94. guid
  95. title
  96. title
  97. link
  98. link
  99. description
  100. description
  101. description
  102. description
  103. pubDate
  104. pubDate
  105. enclosure
  106. category
  107. category
  108. category
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement