Created
October 15, 2020 18:45
-
-
Save foxicode/2c0d2715d750cdca8fee4fc0f03d11ad to your computer and use it in GitHub Desktop.
Parsing XML in Swift
This file contains hidden or 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 | |
| class ParserDelegate: NSObject, XMLParserDelegate { | |
| func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { | |
| print("Found element \(elementName) with attributes \(attributeDict)") | |
| } | |
| } | |
| let delegate = ParserDelegate() | |
| let xml = "<parent><child attr=\"attr\" /></parent>" | |
| let xmlData = xml.data(using: .utf8)! | |
| let parser = XMLParser(data: xmlData) | |
| parser.delegate = delegate | |
| parser.parse() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment