Swift Parsing-Attributname für den angegebenen Elementnamen
Hier ist ein Teil meiner 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 gibt den Namen der Stadt ein, zum Beispiel:"Пицунда"
und ich möchte seine ID erhalten. Zum"Пицунда"
id ist"10282"
.
Below Ich habe meinen nicht funktionierenden Code gepostet.
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 any resources that can be 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!) {
}
Schließlich möchte ich die ID an eine andere URL-Datei übergeben(export.yandex.ru/weather-ng/forecasts/{id of the city}.xml)
und analysiere es. Muss ich eine weitere Swift-Klasse erstellen und sie irgendwie mit der ersten verbinden?