swift tableview xmlparser 스위프트 테이블뷰 xml 파싱 



//

//  ViewController.swift

//  WeatherTable

//

//  Created by stayfoolish on 21/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UITableViewDataSource, XMLParserDelegate  {

    var datalist: [[String:String]] = []

    var detaildata : [String: String] = [:]

    var elementTemp: String = ""

    var blank: Bool = false

 

    


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let baseURL = "https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.xml"

        let parser = XMLParser(contentsOf: URL(string: baseURL)!)

        

        parser?.delegate = self

        parser?.parse()

    }

    

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {

//        print("didStartElement : \(elementName)")

        elementTemp = elementName

        blank = true

    }

    

    func parser(_ parser: XMLParser, foundCharacters string: String) {

        if blank == true && elementTemp != "local" && elementTemp != "weatherinfo" {

//            print("foundCharacters : \(string)")

            detaildata[elementTemp] = string

        }


    }

    

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

        if elementName == "local"{

            datalist += [detaildata]

            print(detaildata)

        }

        blank = false

//        print("didEndElement : \(elementName)")

    }

    

    


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return datalist.count

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WeatherCell

//        print("indexPath : \(indexPath)")

//        print("indexPath row : \(indexPath.row)")

        

        var dicTemp = datalist[indexPath.row]

        

//        print(dicTemp)

        

        cell.countryLabel.text = dicTemp["country"]

        

        let weatherStr = dicTemp["weather"]

        

        cell.weatherLabel.text = weatherStr

        cell.temperatureLabel.text = dicTemp["temperature"]

        

        if weatherStr == "맑음"{

            cell.imgView?.image = UIImage(named: "sunny.png")

        }else if weatherStr == "비"{

            cell.imgView?.image = UIImage(named: "rainy.png")

        }else if weatherStr == "흐림"{

            cell.imgView?.image = UIImage(named: "cloudy.png")

        }else if weatherStr == "눈"{

            cell.imgView?.image = UIImage(named: "snow.png")

        }else {

            cell.imgView?.image = UIImage(named: "blizzard.png")

            

        }

        

        

        return cell

    }

    

    

}




//

//  WeatherCell.swift

//  WeatherTable

//

//  Created by stayfoolish on 22/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class WeatherCell: UITableViewCell {


    @IBOutlet var countryLabel: UILabel!

    @IBOutlet var weatherLabel: UILabel!

    @IBOutlet var temperatureLabel: UILabel!

    @IBOutlet var imgView: UIImageView!

    

    override func awakeFromNib() {

        super.awakeFromNib()

        // Initialization code

    }


    override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)


        // Configure the view for the selected state

    }


}


xml url 내용


<weatherinfo>
  <local>
    <country>한국</country>
    <weather>비</weather>
    <temperature>20</temperature>
  </local>
  <local>
    <country>일본</country>
    <weather>맑음</weather>
    <temperature>19</temperature>
  </local>
  <local>
    <country>중국</country>
    <weather>눈</weather>
    <temperature>14</temperature>
  </local>
  <local>
    <country>스페인</country>
    <weather>우박</weather>
    <temperature>13</temperature>
  </local>
  <local>
    <country>미국</country>
    <weather>흐림</weather>
    <temperature>2</temperature>
  </local>
  <local>
    <country>영국</country>
    <weather>비</weather>
    <temperature>10</temperature>
  </local>
  <local>
    <country>프랑스</country>
    <weather>흐림</weather>
    <temperature>15</temperature>
  </local>
  <local>
    <country>브라질</country>
    <weather>흐림</weather>
    <temperature>35</temperature>
  </local>
  <local>
    <country>스위스</country>
    <weather>맑음</weather>
    <temperature>13</temperature>
  </local>
  <local>
    <country>덴마크</country>
    <weather>비</weather>
    <temperature>2</temperature>
  </local>
  <local>
    <country>스웨덴</country>
    <weather>눈</weather>
    <temperature>0</temperature>
  </local>
  <local>
    <country>네덜란드</country>
    <weather>비</weather>
    <temperature>12</temperature>
  </local>
  <local>
    <country>크로아티아</country>
    <weather>맑음</weather>
    <temperature>30</temperature>
  </local>
  <local>
    <country>필리핀</country>
    <weather>맑음</weather>
    <temperature>28</temperature>
  </local>
  <local>
    <country>독일</country>
    <weather>눈</weather>
    <temperature>3</temperature>
  </local>
  <local>
    <country>헝가리</country>
    <weather>비</weather>
    <temperature>13</temperature>
  </local>
  <local>
    <country>벨기에</country>
    <weather>흐림</weather>
    <temperature>8</temperature>
  </local>
  <local>
    <country>핀란드</country>
    <weather>우박</weather>
    <temperature>15</temperature>
  </local>
  <local>
    <country>이탈리아</country>
    <weather>맑음</weather>
    <temperature>23</temperature>
  </local>  
</weatherinfo>







+ Recent posts