英文:
How to parse response of Web service in Ios?
问题
如何解析iOS中的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?
答案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 == "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=\"htp://tempuri.org\"><string>4300</string><string>4200</string></ArrayOfString>"
let parse = MyParser()
parse.parseXML(xmlString: xmlString)
print(parse.arrStr)
Hope this will help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论