Created
May 21, 2018 15:06
-
-
Save loromits/8a52924fc2c0dfc209f29e86e8405096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class XMLElementExtractor: NSObject, XMLParserDelegate { | |
private var isTarget = false | |
private var value: String? | |
private let elementName: String | |
private init(elementName: String) { | |
self.elementName = elementName | |
} | |
static func extractElement(_ elementName: String, fromXML data: Data) -> String? { | |
let extractor = XMLElementExtractor(elementName: elementName) | |
let parser = XMLParser(data: data) | |
parser.delegate = extractor | |
parser.parse() | |
return extractor.value | |
} | |
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { | |
isTarget = elementName == self.elementName | |
} | |
func parser(_ parser: XMLParser, foundCharacters string: String) { | |
if isTarget { | |
value = value?.appending(string) ?? string | |
} | |
} | |
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { | |
if elementName == self.elementName { | |
parser.abortParsing() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment