iOS swift中的xml解析

前端之家收集整理的这篇文章主要介绍了iOS swift中的xml解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Aadhar卡的条形码或QR码扫描.我得到的响应为以下xml格式.如何使用xml解析将其转换为字典格式?
<?xml version="1.0" encoding="UTF-8"?><PrintLetterBarcodeData uid="685860050795" name="Sangeetha D" gender="F" yob="1989" co="W/O: Dhanansekaran" house="632" street="saradhambal nagar" lm="agaramel" vtc="Nazarathpettai" po="Nazarethpettai" dist="Tiruvallur" subdist="Poonamallee" state="Tamil Nadu" pc="600123" dob="03/06/1989"/>

我尝试使用以下代码进行解析

public func parser(parser: NSXMLParser,didStartElement elementName: String,namespaceURI: String?,qualifiedName qName: String?,attributes attributeDict: [String : String]) {
    currentElement=elementName;

    print(currentElement)


}

public func parser(parser: NSXMLParser,didEndElement elementName: String,qualifiedName qName: String?) {
    currentElement="";

}

public func parser(parser: NSXMLParser,foundCharacters string: String) {

}

但它总是将currentElement作为“PrintLetterBarcodeData”返回

解决方法

这是我在Swift 3中编写的一些解析代码,基于我之前在Swift 2.0中编写的 Google News RSS reader.我修改了这段代码来处理PrintLetterBarcodeData元素列表以及单个元素:
class BarcodeData {
    var uid: String
    var name: String
    var gender: String
    var yob: String
    var co: String
    var house: String
    var street: String
    var lm: String
    var vtc: String
    var po: String
    var dist: String
    var subdist: String
    var state: String
    var pc: String
    var dob: String

    init?(dictionary: [String : String]) {
        guard let uid = dictionary["uid"],let name = dictionary["name"],let gender = dictionary["gender"],let yob = dictionary["yob"],let co = dictionary["co"],let house = dictionary["house"],let street = dictionary["street"],let lm = dictionary["lm"],let vtc = dictionary["vtc"],let po = dictionary["po"],let dist = dictionary["dist"],let subdist = dictionary["subdist"],let state = dictionary["state"],let pc = dictionary["pc"],let dob = dictionary["dob"] else {
            return nil
        }

        self.uid = uid
        self.name = name
        self.gender = gender
        self.yob = yob
        self.co = co
        self.house = house
        self.street = street
        self.lm = lm
        self.vtc = vtc
        self.po = po
        self.dist = dist
        self.subdist = subdist
        self.state = state
        self.pc = pc
        self.dob = dob

    }
}

class MyParser: NSObject {
    var parser: XMLParser

    var barcodes = [BarcodeData]()

    init(xml: String) {
        parser = XMLParser(data: xml.data(using: String.Encoding.utf8)!)
        super.init()
        parser.delegate = self
    }

    func parseXML() -> [BarcodeData] {
        parser.parse()
        return barcodes
    }

}

extension MyParser: XMLParserDelegate {

    func parser(_ parser: XMLParser,attributes attributeDict: [String : String]) {

        if elementName == "PrintLetterBarcodeData" {

            if let barcode = BarcodeData(dictionary: attributeDict) {
                barcodes.append(barcode)
            }
        }
    }
}

用法

let xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PrintLetterBarcodeData uid=\"685860050795\" name=\"Sangeetha D\" gender=\"F\" yob=\"1989\" co=\"W/O: Dhanansekaran\" house=\"632\" street=\"saradhambal nagar\" lm=\"agaramel\" vtc=\"Nazarathpettai\" po=\"Nazarethpettai\" dist=\"Tiruvallur\" subdist=\"Poonamallee\" state=\"Tamil Nadu\" pc=\"600123\" dob=\"03/06/1989\"/>"

let parser = MyParser(xml: xmlString)
let barcodes = parser.parseXML() // array of barcodes
barcodes.first // your barcode
原文链接:https://www.f2er.com/iOS/334061.html

猜你在找的iOS相关文章