NSXMLParser Simple Example

后端 未结 4 517
说谎
说谎 2020-12-24 10:50

Most of the examples of how to invoke the NSXMLParser are contained within complex projects involving Apps. What does a simple example that demonstrates the callbacks look l

4条回答
  •  醉梦人生
    2020-12-24 11:48

    Here is a Swift version of the original Objective-C code below.

    It was built and tested using XCode 7.3. In writing the delegate I found it quite handy to copy the function prototypes from the documentation. It is worth noting that Swift is currently a fairly rapidly moving target.

    main.swift

    import Foundation
    
    // Let's go
    print("Main: Started")
    
    // Try to load the file. Display the description of the error if one occurs
    var xmlData : NSData
    do {
        xmlData = try NSData(contentsOfFile: "/Users/amt/Documents/TestXml/Test.xml",
                             options: .DataReadingMappedIfSafe)
    }
    catch let error as NSError {
        print("Main: \(error.description)")
    
        exit(1)
    }
    
    // Create a parser and point it at the NSData object containing
    // the file we just loaded
    var parser : NSXMLParser! = NSXMLParser(data: xmlData)
    
    // Create a parser delegate object and assign it to the parser
    // Beware the "weak" reference and don't try to combine the two lines
    // of code into one unless you like EXC_BAD_ACCESS exceptions
    var parserDelegate : MyXmlParserDelegate = MyXmlParserDelegate()
    parser.delegate = parserDelegate
    
    // This example also illustrates some of the namespace functions defined in
    // the delegate protocol so enable namespace reporting to see them invoked
    parser.shouldReportNamespacePrefixes = true
    
    // Parse the document
    if !parser.parse() {
        // If parse() returned false then an error occurred so display is location
        // and details
        let error = parser.parserError
        let line = parser.lineNumber
        let col = parser.columnNumber
        print("Parsing failed at \(line):\(col): \(error?.localizedDescription)")
    }
    
    // All done
    print("Main: Ended")
    exit(0)
    

    MyXmlParserDelegate.swift

    import Foundation
    
    class MyXmlParserDelegate:NSObject, NSXMLParserDelegate {
    
        @objc func parserDidStartDocument(parser: NSXMLParser) {
            print("parserDidStartDocument")
        }
    
        @objc func parser(parser: NSXMLParser, didStartElement elementName: String,
                   namespaceURI: String?, qualifiedName qName: String?,
                   attributes attributeDict: [String : String]) {
            print("didStartElement       --> \(elementName)")
        }
    
        @objc func parser(parser: NSXMLParser, foundCharacters string: String) {
            print("foundCharacters       --> \(string)")
        }
    
        @objc func parser(parser: NSXMLParser, didEndElement elementName: String,
                   namespaceURI: String?, qualifiedName qName: String?) {
            print("didEndElement         --> \(elementName)")
        }
    
        @objc func parser(parser: NSXMLParser, didStartMappingPrefix prefix: String,
                   toURI namespaceURI: String) {
            print("didStartMappingPrefix --> Prefix: \(prefix) toURI: \(namespaceURI)")
        }
    
        @objc func parser(parser: NSXMLParser, didEndMappingPrefix prefix: String) {
            print("didEndMappingPrefix   --> Prefix: \(prefix)")
        }
    
        @objc func parserDidEndDocument(parser: NSXMLParser) {
            print("parserDidEndDocument")
        }
    }
    

提交回复
热议问题