Go to file
Helge Hess cbcef1c830
Bump Xcode upgrade markers
...
2021-01-05 15:54:59 +01:00
.github/workflows Drop Travis, use GH Actions 2021-01-05 15:51:53 +01:00
Sources Use an explicit int cast 2021-01-05 15:53:35 +01:00
SwiftyExpat Move Sources into Sources dir 2018-04-27 13:07:33 +02:00
SwiftyExpat.xcodeproj Bump Xcode upgrade markers 2021-01-05 15:54:59 +01:00
SwiftyExpatTests In SPM Expat is an own module, in Xcode embedded 2018-04-27 13:34:11 +02:00
.gitignore Drop Travis, use GH Actions 2021-01-05 15:51:53 +01:00
Package.swift Add a Package.swift 2018-04-27 13:39:26 +02:00
README.md Fix badge 2019-10-04 16:46:32 +02:00

README.md

SwiftyExpat

Swift2 Swift4 Swift5 macOS tuxOS Travis

Simple wrapper for the Expat XML parser.

ChangeLog

2019-10-04: Update to work w/ Swift 5.1 (aka Xcode 11).

2018-04-27: Updated to work w/ Swift 4.0.3 (aka Xcode 9.2). Support for Swift Package Manager.

2015-12-09: Updated to work w/ Swift 2.1.1 (aka Xcode 7.3?).

20xx-yy-zz: Updated to work w/ Swift v0.2b5 (aka Xcode 7b5).

Note: The SwiftyExpat version for Swift 1.x was using a modified Expat which used blocks instead of C function pointer callbacks. Swift 2 now supports C function pointer calls and hence this project got rewritten for this.

Targets

The project includes two targets:

  • SwiftyExpat
  • SwiftyExpatTests

(the SPM setup has Expat as an own target)

I suggest you start by looking at the SwiftyExpatTests.

SwiftyExpat

This is a tiny framework wth a small Swift class to make the API nicer. Though this is not really necessary - Expat is reasonably easy to use from Swift as-is.

let p = Expat()
  .onStartElement   { name, attrs in print("<\(name) \(attrs)")       }
  .onEndElement     { name        in print(">\(name)")                }
  .onStartNamespace { prefix, uri in print("+NS[\(prefix)] = \(uri)") }
  .onEndNamespace   { prefix      in print("-NS[\(prefix)]")          }
  .onError          { error       in print("ERROR: \(error)")         }
p.write("<hello>world</hello>")
p.close()

The raw Expat API works like this:

let p = XML_ParserCreate("UTF-8")
defer { XML_ParserFree(p) }

XML_SetStartElementHandler(p) { _, name, attrs in
  print("start tag \(String.fromCString(name)!)")
}
XML_SetEndElementHandler  (p) { _, name in
  print("end tag \(String.fromCString(name)!)")
}

XML_Parse(p, "<hello/>", 8, 0)
XML_Parse(p, "", 0, 1)

You get the idea ...

Note: The closures in the raw API cannot capture variables. If you need to pass around context (very likely ...), you need to fill the regular Expat 'user data' field (which the wrapper does, if you need an example).

SwiftyExpatTests

Just a tiny demo on how to invoke the parser.

Contact

@helje5 | helge@alwaysrightinstitute.com