Если бы вы просто использовали XMLParser
, вы бы определили объект делегата:
class ParserDelegate: NSObject, XMLParserDelegate {
var currentValue: String?
var registrationResult: String?
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
if elementName == "QR_GetDeviceCheckAndRegistrationResult" {
currentValue = ""
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
currentValue? += string
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "QR_GetDeviceCheckAndRegistrationResult" {
registrationResult = currentValue
currentValue = nil
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print(parseError)
}
}
А потом разберешься:
let parser = XMLParser(data: data)
let parserDelegate = ParserDelegate()
parser.delegate = parserDelegate
guard
parser.parse(),
let result = parserDelegate.registrationResult
else { return }
print(result)