Skip to content

hironytic/XMLPullitic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XMLPullitic

CI Status Version License Platform Carthage compatible

Swifty XML pull parser

Usage

let xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><foo>Hello World!</foo>"
if let parser = XMLPullParser(string: xml) {
    do {
        parsing: while true {
            switch try parser.next() {
            case .startDocument:
                print("start document")
            case .startElement(let name, _, _):
                print("start element: \(name)")
            case .characters(let text):
                print("text: \(text)")
            case .endElement(let name, _):
                print("end element: \(name)")
            case .endDocument:
                print("end document")
                break parsing
            }
        }
    } catch let error {
        print("error: \(error)")
    }
}

Thanks to swift's pattern matching feature, you can easily handle start of elements like this:

let event = try parser.next()
switch event {
case .startElement("foo", _, _):
    // handle start of <foo>
    print("foo: \(event)")
case .startElement("bar", _, _):
    // handle start of <bar>
    print("bar: \(event)")
case .startElement("baz", "http://example.com/ns/1.0"?, let element):
    // handle start of <baz>, whose namespace URI is http://example.com/ns/1.0
    // note: you must set parser.shouldProcessNamespaces true to handle namespaces
  
    // you can get attributes from XmlElement object
    print(element.attributes["xyz"])
case .startElement(_, _, _):
    // handle start of other elements
    print("other: \(event)")
default:
    break
}

Requirements

  • iOS 8.0+
  • tvOS 9.0+
  • Swift 5.1+

Installation

CocoaPods

XMLPullitic is available through CocoaPods. To install it, simply add the following lines to your Podfile:

use_frameworks!
pod "XMLPullitic", '~> 3.0'

Carthage

XMLPullitic is available through Carthage. To install it, simply add the following line to your Cartfile:

github "hironytic/XMLPullitic" ~> 3.0

Author

Hironori Ichimiya, [email protected]

License

XMLPullitic is available under the MIT license. See the LICENSE file for more info.