Swift XML attribute

Hello! Could you please help me with a Swift XML problem?

I have a xml file from ftp that I want to extract data from xml attribute.

How can I show the data from a XML attribute on a Swift App?


Thank you!

The standard tool for this in XMLParser. Pasted in below is a trivial example of how to use this on the Mac. I don’t know of any official examples written in Swift, but the SeismicXML shows a more detailed example in Objective-C.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
import Foundation

class XMLTest : NSObject, XMLParserDelegate {

    func test(url: URL) {
        let parser = XMLParser(contentsOf: url)!
        parser.delegate = self
        let success = parser.parse()
        if success {
            print("done")
        } else {
            print("error \(parser.parserError!)")
        }
    }

    var depth = 0
    var depthIndent: String {
        return [String](repeating: "  ", count: self.depth).joined()
    }

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        print("\(self.depthIndent)>\(elementName)")
        self.depth += 1
    }

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        self.depth -= 1
        print("\(self.depthIndent)<\(elementName)")
    }
}

// This path is not considered API; it was just a convenient XML file
// lying around on my hard disk that I used for this example.

XMLTest().test(url: URL(fileURLWithPath: "/Library/Dictionaries/Apple Dictionary.dictionary/Contents/Resources/English.lproj/AppleDictionary.xml"))
Swift XML attribute
 
 
Q