如何解析iOS中Web服务的响应?

huangapple go评论78阅读模式
英文:

How to parse response of Web service in Ios?

问题

如何解析iOS中的Web服务响应?

这是我的Web服务响应

我已经知道如何使用Alamofire解析JSON响应,但如何解析这个响应并将值存入数组中?

这是我尝试的代码

英文:

How to parse response of Web service in Ios?

Here is the Response of my web service

i already know how to parse json response by using alamofire but how to parse this response and get value in array?

Here is my tried code

答案1

得分: 1

你可以使用XMLParser进行XML解析。

以下是Swift代码:

class MyParser: NSObject, XMLParserDelegate {
    
    var elementName: String = String()
    var arrStr: [String] = []

    func parseXML(xmlString: String) {
        if let xmlData = xmlString.data(using: .utf8) {
            let parser = XMLParser(data: xmlData)
            parser.delegate = self
            parser.parse()
        }
    }
    
    // MARK:- XMLParserDelegate callbacks
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        self.elementName = elementName
    }
    
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        if !data.isEmpty {
            if self.elementName == "string" {
                arrStr.append(data)
            }
        }
    }
}

let xmlString = "<ArrayOfString xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://tempuri.org\"><string>4300</string><string>4200</string></ArrayOfString>"

let parse = MyParser()
parse.parseXML(xmlString: xmlString)
print(parse.arrStr)

希望这对你有所帮助。

英文:

You can do XML Parsing using XMLParser.

Below is the code in Swift:

class MyParser : NSObject, XMLParserDelegate{
    
    var elementName: String = String()
    var arrStr: [String] = []

    func parseXML(xmlString : String){
        if let xmlData = xmlString.data(using: .utf8){
            let parser = XMLParser(data: xmlData)
            parser.delegate = self;
            parser.parse()
        }
    }
    
    // MARK:- XMLParserDelegate callbacks
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        self.elementName = elementName
    }
    
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        if (!data.isEmpty) {
            if self.elementName == &quot;string&quot; {
                arrStr.append(data)
            }
        }
    }
}

let xmlString = &quot;&lt;ArrayOfString xmlns:xsd=\&quot;http://www.w3.org.2001/XMLSchema\&quot; xmlns:xsi=\&quot;http://www.w3.org.2001/XMLSchema-instance\&quot; xmlns=\&quot;htp://tempuri.org\&quot;&gt;&lt;string&gt;4300&lt;/string&gt;&lt;string&gt;4200&lt;/string&gt;&lt;/ArrayOfString&gt;&quot;

let parse = MyParser()
parse.parseXML(xmlString: xmlString)
print(parse.arrStr)

Hope this will help.

huangapple
  • 本文由 发表于 2020年1月3日 14:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定