Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created October 15, 2020 18:45
Show Gist options
  • Select an option

  • Save foxicode/2c0d2715d750cdca8fee4fc0f03d11ad to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/2c0d2715d750cdca8fee4fc0f03d11ad to your computer and use it in GitHub Desktop.
Parsing XML in Swift
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