Swift parsing attribute name for given elementName -
here part of url
<cities> <country name="Абхазия"> <city id="37188" region="27028" head="" type="3" country="Абхазия" part="" resort="" climate="">Новый Афон</city> <city id="37178" region="10282" head="" type="3" country="Абхазия" part="" resort="" climate="">Пицунда</city> <city id="37187" region="37187" head="" type="3" country="Абхазия" part="" resort="" climate="">Гудаута</city> <city id="37172" region="10280" head="" type="3" country="Абхазия" part="" resort="" climate="">Гагра</city> <city id="37189" region="10281" head="0" type="3" country="Абхазия" part="" resort="0" climate="">Сухум</city> </country>
user types name of city, example: "Пицунда"
, want id. "Пицунда"
id "10282"
.
below i've posted not-working code.
var parser: nsxmlparser! var city: string = string() var ifdirok = false var ifcitynameok = false override func viewdidload() { super.viewdidload() let url: nsurl = nsurl(string: "https://pogoda.yandex.ru/static/cities.xml")! parser = nsxmlparser(contentsofurl: url) parser.delegate = self parser.parse() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func parser(parser: nsxmlparser!, didstartelement elementname: string!, namespaceuri: string!, qualifiedname qname: string!, attributes attributedict: [nsobject : anyobject]!) { //let cityid = attributedict ["id"] as? nsstring if (elementname == "city"){ ifdirok = true } } func parser(parser: nsxmlparser!, foundcharacters string: string!) { var data = string.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) if (data == city){ ifcitynameok = true } } func parser(parser: nsxmlparser!, foundattributedeclarationwithname attributename: string!, forelement elementname: string!, type: string!, defaultvalue: string!) { if (ifdirok && ifcitynameok){ println("\(attributename)") } } func parser(parser: nsxmlparser!, didendelement elementname: string!, namespaceuri: string!, qualifiedname qname: string!) { }
after all, want pass id url file (export.yandex.ru/weather-ng/forecasts/{id of city}.xml)
, parse it. need create swift class , somehow connect first one?
building dictionary of [city:id] can solution you. have implemented simple solution based on article lifecycle of nsxmlparser @ http://www.codeproject.com/articles/248883/objective-c-fundamentals-nsxmlparser .
following method called when when element starting. can retrieve city id attribute , save in instance level variable can use in next method.
func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [nsobject : anyobject])
and then, following method called when parser see between starting , ending.
func parser(parser: nsxmlparser!, foundcharacters string: string!)
so, can city name here. have city id , city name add new item [city:id] dictionary.
once build dictionary, searching simple.
here working test code.
class viewcontroller: uiviewcontroller ,nsxmlparserdelegate{ var parser: nsxmlparser! var city: string = string() var ifdirok = false var ifcitynameok = false var element : string? var value: string=string() var dic = dictionary<string,string>() var currentcityid:string? @iboutlet weak var result: uilabel! @iboutlet weak var search: uitextfield! //search text @ibaction func actiongogetit(sender: anyobject) { self.result.text=dic[self.search.text] } override func viewdidload() { super.viewdidload() let url: nsurl = nsurl(string: "https://pogoda.yandex.ru/static/cities.xml")! parser = nsxmlparser(contentsofurl: url) parser.delegate = self parser.parse() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [nsobject : anyobject]) { element = elementname if (element == "city"){ ifdirok = true let cityid = attributedict ["id"] as? nsstring self.currentcityid = cityid as? string } } func parser(parser: nsxmlparser!, foundcharacters string: string!) { var data = string.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) if (!data.isempty){ if (element == "city"){ dic[data] = self.currentcityid string? } } } func parser(parser: nsxmlparser, foundattributedeclarationwithname attributename: string, forelement elementname: string, type: string?, defaultvalue: string?) { if (ifdirok && ifcitynameok){ println("\(attributename)") } } func parser(parser: nsxmlparser, didendelement elementname: string, namespaceuri: string?, qualifiedname qname: string?) { } }
Comments
Post a Comment