简体   繁体   English

swift 2如何在解析的xml变量中使用正则表达式获取时间模式

[英]How to get time pattern using regex in a parsed xml variable in swift 2

I have a rss feed which I parse it's data using SWXMLHash, there are three type of data which I need from this feeds,Image src , title , time. 我有一个RSS提要,我使用SWXMLHash解析它的数据,从此提要中需要三种类型的数据:Image src,title,time。

I don't have any problem with getting images and title but there is a bit problem in getting time value. 我在获取图像和标题方面没有任何问题,但是在获取时间值方面存在一些问题。

my rss feed time formate is like : 我的rss进给时间formate是这样的:

<item>
          <title>item subject</title>
          <description>
          <img align="right" hspace="5" src="http://test.org/index/uploads/conductor/thumbnails/0192.jpg"/>
          </description>
          <pubDate>2015-11-14 05:05:03</pubDate>
  </item>

problem is that pubDate tag contains date + time , but I need just the time. 问题是pubDate标记包含date + time,但是我只需要时间。 is there a way to find xxxx-xx-xx pattern and replace it with '' Or find xx:xx:xx which is time? 有没有办法找到xxxx-xx-xx模式并将其替换为''或找到xx:xx:xx时间?

this is how I parse my rss feed : 这就是我解析我的rss feed的方式:

struct sprog {
        var spTitle : String!
        var spImg : String!
        var spTime : String!
    }

var tableData = [sprog]()


override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://razavitv.aqr.ir/index/conductor_module/rss")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in      
        let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
        let processedString = (dataString as NSString).stringByReplacingOccurrencesOfString("<![CDATA[", withString: "").stringByReplacingOccurrencesOfString("]]>", withString: "") as String


        let data: NSData = processedString.dataUsingEncoding(NSUTF8StringEncoding)!
        let xml = SWXMLHash.parse(data)

        //one root element
        let count = xml["rss"]["channel"]["item"].all.count

        for var i = 0; i < count; i++ {
            let appName = xml["rss"]["channel"]["item"][i]["title"].element!.text!
            let appUrl = xml["rss"]["channel"]["item"][i]["description"]["img"].element!.attributes["src"]
            let appTime = xml["rss"]["channel"]["item"][i]["pubDate"].element!.text!


            let ap = sprog(spTitle: appName , spImg : appUrl , spTime : appTime)
            self.tableData.append(ap)

            //for reducing delay of loading table view data
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
                return
            })

        }

    }

    task.resume()

}

Don't use regex to turn a string into a date — date/time formatting and calculation is a much hairier problem than it ever appears. 请勿使用正则表达式将字符串转换为日期-日期/时间的格式和计算比以往任何时候都更加麻烦。 What time zone are you dealing with? 您正在处理哪个时区? What time zone is the user in? 用户在哪个时区? Is it a leap year? 是a年吗? Is the date you're reading valid, even if it is/isn't a leap year? 即使是/不是date年,您正在阅读的日期是否有效? And that's all assuming you're dealing with a fixed format coming from the RSS feed... it gets even more fun when you're dealing with date strings that could come out of end-user-configured software. 所有这些都假设您正在处理来自RSS feed的固定格式...当您处理可能来自最终用户配置的软件的日期字符串时,它会变得更加有趣。

Instead of trying to tackle that yourself, let the system do it for you. 让系统代替您自己解决问题,而不是尝试自己解决。 Use a date formatter to read the date (see "Parsing Fixed Format Date Representations" on that page for an example. Then, if you don't need parts of the date (like the day/month/year), create an NSDateComponents object from the date and read from it only the fields (hour/minute/etc) you're interested in. 使用日期格式器读取日期(有关示例,请参见该页面上的“解析固定格式的日期表示形式”。然后,如果不需要日期的一部分(如日/月/年),则创建一个NSDateComponents对象从日期开始,仅读取您感兴趣的字段(小时/分钟/等)。

Why Don't you use split ? 为什么不使用split? your can split your variable and store time and date in different variables : 您可以拆分变量并将时间和日期存储在不同的变量中:

//seperate string by space
                var apptime_array = appTime.componentsSeparatedByString(" ")
                //seperate string by :
                apptime_array = apptime_array[1].componentsSeparatedByString(":")
                appTime = "\(apptime_array[0]):\(apptime_array[1])"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM