3

I am new in swift programming .. I want to parse the xml data in swift..

this is my xml data..

  <rss version="2.0">
     <channel>
     <title>Zee News :India National</title>
     <link>
     <![CDATA[ http://zeenews.india.com ]]>
     </link>
    <description>
    <![CDATA[
        Visit Zee News for latest India news, get latest news from India and        all over the world. We provide you latest celebrity news, latest bollywood news and entertainment news with fastest top stories online about cricket, sports, business, bollywood, entertainment, lifestyle, world and science to get yourself updated.
    ]]>
    </description>
    <language>en-us</language>
    <copyright>Zee News Ltd</copyright>
    <category>India National News</category>
   <pubDate>
   <![CDATA[ 2/04/2016 11:16:57 AM GMT ]]>
   </pubDate>
   <image>
   <url>
   <![CDATA[
http://znn.india.com/images/logo-zeenews-aab-nw.jpg
]]>
   </url>
   <title>
   <![CDATA[ Zee News ]]>
   </title>
   <link>
   <![CDATA[ http://zeenews.india.com ]]>
   </link>
   </image>
   <item>
   <title>
Son of retired Major General arrested in Goa for suspected `terror links`
   </title>
   <link>
   <![CDATA[
http://zeenews.india.com/news/india/son-of-retired-major-general-arrested-in-goa-for-suspected-terror-links_1852160.html
]]>
   </link>
   <description>
Samir Sardana was picked up from Vasco railway station after he was found "wandering suspiciously".
   </description>
<comments>mailto:inewsonline@gmail.com</comments>
<pubdate>Thursday, February 04, 2016, 10:13 GMT +5:30</pubdate>
   <author/>
<guid isPermaLink="false">
   <![CDATA[
http://zeenews.india.com/news/india/son-of-retired-major-general-arrested-in-goa-for-suspected-terror-links_1852160.html
]]>
   </guid>
   </item>

I tried to parse but only "item" data can parse not the "title" & "image"

here is my code...

     var parser = NSXMLParser()
    var posts = NSMutableArray()

    var elements = NSMutableDictionary()

    var element = NSString()
    var title1 = NSMutableString()
    var title3 = NSMutableString()
    var date = NSMutableString()
    var link = NSMutableString()
func beginParsing()
    {

        posts = []
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://zeenews.india.com/rss/india-national-news.xml"))!)!
        parser.delegate = self
        parser.parse()

        tbData!.reloadData()
    }
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?,attributes attributeDict:[NSObject :AnyObject])
    {
if (elementName as NSString).isEqualToString("title")
        {
        elements = NSMutableDictionary.alloc()
            title3 = NSMutableString()
            title3 = ""
         }

      if (elementName as NSString).isEqualToString("item")
        {
            elements = NSMutableDictionary.alloc()
            elements = [:]
            title1 = NSMutableString()
            title1 = ""
            date = NSMutableString()
            date = ""
            link = NSMutableString()
            link = ""
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
    {
if (elementName as NSString).isEqualToString("title")
        {
            if !title3.isEqual(nil) {
               elements.setObject(title3, forKey: "title")
                //posts.addObject(elements)

       }

       }

      if (elementName as NSString).isEqualToString("item") {
            if !title1.isEqual(nil) {
                elements.setObject(title1, forKey: "title")
            }
            if !date.isEqual(nil) {
                elements.setObject(date, forKey: "date")
            }
            if !link.isEqual(nil){
               elements.setObject(link, forKey: "link")
            }
            posts.addObject(elements)
        }
        //posts.addObject(elements)
        //posts.addObject(post)
    }

    func parser(parser: NSXMLParser, foundCharacters string: String?)
    {
        if element.isEqualToString("title")
       {
       title3.appendString(string!)
       }


       if element.isEqualToString("title") {
            title1.appendString(string!)
        }
         if element.isEqualToString("link"){
                link.appendString(string!)
        }

         if element.isEqualToString("pubDate") {
            date.appendString(string!)
        }
    }

Plz... suggest any solution how i can parse the entire rss feed data...

Ruhi
  • 137
  • 2
  • 5
  • Possible duplicate of [How Do you make a RSS Reader using Swift](http://stackoverflow.com/questions/24468338/how-do-you-make-a-rss-reader-using-swift) – Abhijeet Feb 05 '16 at 03:21
  • There are few github projects on the parsing XML data. 1. [Link](https://github.com/wantedly/swift-rss-sample) 2. [Link](https://github.com/tibo/SwiftRSS) – Abhijeet Feb 05 '16 at 03:22

1 Answers1

1

I'm purely guessing right now.

You say that item parses fine, whereas title and image does not.

Looking at the XML it seems that image for instance contains only CDATA elements

<image>
   <url>
     <![CDATA[http://znn.india.com/images/logo-zeenews-aab-nw.jpg]]>
   </url>
   <title>
     <![CDATA[ Zee News ]]>
   </title>
   <link>
     <![CDATA[ http://zeenews.india.com ]]>
   </link>
</image>

I found this post here on Stack Overflow where someone asks how to deal with CDATA in XML. It turns out that NSXMLParserDelegate has an optional method called:

parser(_ parser: NSXMLParser, foundCDATA CDATABlock: NSData)

You can read about it here at Apples documentation

Maybe that could help you?

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • Thank u.. for this ... i will try this... But i want to parse the title also ... when i try to parse the title and run , my debugger check both the title in the "func parser(parser: NSXMLParser, foundCharacters string: String?) ", the title which is inside the channel and title inside the item and then error will come .... it does not parse and store in the array.. – Ruhi Feb 06 '16 at 15:24
  • The **NSXMLParser** and **NSXMLParserDelegate** had been renamed to **XMLParser** & **XMLParserDelegate**. – Mihai Petcu Dec 22 '16 at 15:58