SwiftyExpat 是简单的 Expat XML Parser 的封装,可以使用块,而不用函数指针调用。
SwiftyExpat 包括两个部分:
SwiftyExpat
SwiftyExpatTests
let p = Expat() .onStartElement { name, attrs in println("<\(name) \(attrs)") } .onEndElement { name in println(">\(name)") } .onStartNamespace { prefix, uri in println("+NS[\(prefix)] = \(uri)") } .onEndNamespace { prefix in println("-NS[\(prefix)]") } .onError { error in println("ERROR: \(error)") }p.write("<hello>world</hello>")p.close()
Expat API 示例:
var p = XML_ParserCreate("UTF-8") XML_SetStartElementHandler(p) { _, name, attrs in println("start tag \(name)") } XML_SetEndElementHandler (p) { _, name in println("end tag \(name)") } XML_Parse(parser, "<hello/>", 8, 0)XML_Parse(parser, "", 0, 1) XML_ParserFree(p); p = nil