Get attibuteDict of a subclass Xml parsing - Swift

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I want to get attribute of a subclass of my item :

  <pdv id="1000002" latitude="4621842" longitude="522767" cp="01000" pop="R"> <adresse>16 Avenue de Marboz</adresse> <ville>BOURG-EN-BRESSE</ville> <ouverture debut="01:00" fin="01:00" saufjour=""/> <services>   <service>Automate CB</service>   <service>Vente de gaz domestique</service> </services> <prix nom="Gazole" id="1" maj="2015-05-30 11:30:17" valeur="1206"/> <prix nom="SP95" id="2" maj="2015-05-30 11:30:17" valeur="1398"/> <prix nom="SP98" id="6" maj="2015-05-30 11:30:17" valeur="1434"/> <rupture id="3" nom="E85" debut="2009-11-03 12:19:00" fin=""/> <fermeture/></pdv>

I know how to get <pdv id="000"> with attributeDict but I try to get <prix nom="example">

I try with that code :

func beginParsing() {     parser = NSXMLParser(contentsOfURL: testUrl)!     parser.delegate = self     parser.parse() }  func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {     element = elementName     if (elementName as NSString).isEqualToString("pdv")     {          cp = attributeDict["cp"]! as NSMutableString         latitude = attributeDict["latitude"]! as NSMutableString         longitude = attributeDict["longitude"]! as NSMutableString          elements = NSMutableDictionary.alloc()         elements = [:]         PrixCarbu = NSMutableString.alloc()         PrixCarbu = ""      }  }  func parser(parser: NSXMLParser!, foundCharacters string: NSString!) {      if element.isEqualToString("prix") {         PrixCarbu.appendString(string)          func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName: String!, attributes: [NSObject : AnyObject]!) {              prixString = elementName             if (elementName as NSString).isEqual("prix") {                  println(attributes)              }          }      }  }   func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {     if (elementName as NSString).isEqualToString("pdv") {         if !cp.isEqual(nil) {             elements.setObject(cp, forKey: "cp")         }         if !latitude.isEqual(nil) {             elements.setObject(latitude, forKey: "latitude")         }         if !longitude.isEqual(nil) {             elements.setObject(longitude, forKey: "longitude")         }`

Thank's!

回答1:

You should put your "prix" check into the start element method. It is equivalent to "pdv", just use the same mechanism.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!