
<xml> <record> <Empname>A Employee</Empname> <EmpPhone>111-222-3333</EmpPhone> <EmpEmail>a@employee.com</EmpEmail> <EmpAddress>12345 Fake Street</EmpAddress> <EmpAddress1>MyTown,Mystate ZIP</EmpAddress1> </record></xml>
并且只想将其保存为NSDictionary(标记为键和数据作为值).到目前为止,我能够成功完成的是在控制台中打印xml字符串:
let url = NSURL(string: "http://www.urlexample.com/file.xml")let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data,response,error) in println(Nsstring(data: data,enCoding: NSUTF8StringEnCoding))}print(task)task.resume() 我已经完成了我发现的任何在线教程,这些教程要么过时,要么过于复杂.任何帮助表示赞赏.
过程很简单:>创建XMLParser对象,并将数据传递给它.
>指定该解析器的委托.
>启动解析.
所以,在Swift 3/4中,它看起来像:
let task = URLSession.shared.dataTask(with: url) { data,error in guard let data = data,error == nil else { print(error ?? "UnkNown error") return } let parser = XMLParser(data: data) parser.delegate = self if parser.parse() { print(self.results ?? "No results") }}task.resume() 问题是如何实现XMLParserDelegate方法.三个关键方法是dIDStartElement(准备接收字符的地方),foundCharacters(处理解析的实际值)和dIDEndElement(保存结果的地方).
您询问了如何解析单个记录(即单个字典),但我将向您展示解析其中一系列的更一般模式,这是XML的一种常见情况.如果你不需要一个值数组(或者只是抓住第一个值),你显然可以看到如何简化这个.
// a few constants that IDentify what element names we're looking for insIDe the XML// a few constants that IDentify what element names we're looking for insIDe the XMLlet recordKey = "record"let dictionaryKeys = Set<String>(["Empname","EmpPhone","EmpEmail","EmpAddress","EmpAddress1"])// a few variables to hold the results as we parse the XMLvar results: [[String: String]]? // the whole array of dictionarIEsvar currentDictionary: [String: String]? // the current dictionaryvar currentValue: String? // the current value for one of the keys in the dictionary
和
extension VIEwController: XMLParserDelegate { // initialize results structure func parserDIDStartdocument(_ parser: XMLParser) { results = [] } // start element // // - If we're starting a "record" create the dictionary that will hold the results // - If we're starting one of our dictionary keys,initialize `currentValue` (otherwise leave `nil`) func parser(_ parser: XMLParser,dIDStartElement elementname: String,namespaceURI: String?,qualifIEdname qname: String?,attributes attributeDict: [String : String]) { if elementname == recordKey { currentDictionary = [:] } else if dictionaryKeys.contains(elementname) { currentValue = "" } } // found characters // // - If this is an element we care about,append those characters. // - If `currentValue` still `nil`,then do nothing. func parser(_ parser: XMLParser,foundCharacters string: String) { currentValue? += string } // end element // // - If we're at the end of the whole dictionary,then save that dictionary in our array // - If we're at the end of an element that belongs in the dictionary,then save that value in the dictionary func parser(_ parser: XMLParser,dIDEndElement elementname: String,qualifIEdname qname: String?) { if elementname == recordKey { results!.append(currentDictionary!) currentDictionary = nil } else if dictionaryKeys.contains(elementname) { currentDictionary![elementname] = currentValue currentValue = nil } } // Just in case,if there's an error,report it. (We don't want to fly blind here.) func parser(_ parser: XMLParser,parseErrorOccurred parseError: Error) { print(parseError) currentValue = nil currentDictionary = nil results = nil }} 对于Swift 2的再现,请参阅previous revision of this answer.
总结以上是内存溢出为你收集整理的在Swift中从URL解析XML全部内容,希望文章能够帮你解决在Swift中从URL解析XML所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)