我在Swift 3中使用LibXML2的SAX解析器遇到了问题。
我想要在iOS上获得类似于Android中的XMLPullParser
。它可以从服务器下载XML,并在下载时进行流解析。
我的XML看起来像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<ResultList id="12345678-0" platforms="A;B;C;D;E">
<Book id="1111111111" author="Author A" title="Title A" price="9.95" ... />
<Book id="1111111112" author="Author B" title="Title B" price="2.00" ... />
<Book id="1111111113" author="Author C" title="Title C" price="5.00" ... />
<ResultInfo bookcount="3" />
</ResultList>
所有数据都存储在属性中而非子节点。
我自己创建了以下类,主要基于这些示例:
XMLPerformance、XMLPerformance-Swift和iOS-XML-Streaming。
import Foundation
class LibXMLParser: NSObject, URLSessionDataDelegate {
var url: URL?
var delegate: LibXMLParserDelegate?
var done = false
var context: xmlParserCtxtPtr?
var simpleSAXHandlerStruct: xmlSAXHandler = {
var handler = xmlSAXHandler()
handler.initialized = XML_SAX2_MAGIC
handler.startElementNs = startElementSAX
handler.endElementNs = endElementSAX
handler.characters = charactersFoundSAX
//handler.error = errorEncounteredSAX
return handler
}()
init(url: URL) {
super.init()
self.url = url
}
func parse() {
self.done = false
let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
let dataTask = session.dataTask(with: URLRequest(url: url!))
dataTask.resume()
self.context = xmlCreatePushParserCtxt(&simpleSAXHandlerStruct, Unmanaged.passUnretained(self).toOpaque(), nil, 0, nil)
self.delegate?.parserDidStartDocument()
repeat {
RunLoop.current.run(mode: .defaultRunLoopMode, before: Date.distantFuture)
} while !self.done
xmlFreeParserCtxt(self.context)
self.delegate?.parserDidEndDocument()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
print("Did receive data")
data.withUnsafeBytes { (bytes: UnsafePointer<CChar>) -> Void in
xmlParseChunk(self.context, bytes, CInt(data.count), 0)
}
}
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
xmlParseChunk(self.context, nil, 0, 1)
self.done = true
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
self.done = true
//self.delegate?.parserErrorOccurred(error)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
self.done = true
//self.delegate?.parserErrorOccurred(error)
}
}
private func startElementSAX(_ ctx: UnsafeMutableRawPointer?, name: UnsafePointer<xmlChar>?, prefix: UnsafePointer<xmlChar>?, URI: UnsafePointer<xmlChar>?, nb_namespaces: CInt, namespaces: UnsafeMutablePointer<UnsafePointer<xmlChar>?>?, nb_attributes: CInt, nb_defaulted: CInt, attributes: UnsafeMutablePointer<UnsafePointer<xmlChar>?>?) {
let parser = Unmanaged<LibXMLParser>.fromOpaque(ctx!).takeUnretainedValue()
parser.delegate?.parserDidStartElement(String(cString: name!), nb_attributes: nb_attributes, attributes: attributes)
}
private func endElementSAX(_ ctx: UnsafeMutableRawPointer?, name: UnsafePointer<xmlChar>?,
prefix: UnsafePointer<xmlChar>?,
URI: UnsafePointer<xmlChar>?) {
let parser = Unmanaged<LibXMLParser>.fromOpaque(ctx!).takeUnretainedValue()
parser.delegate?.parserDidEndElement(String(cString: name!))
}
private func charactersFoundSAX(_ ctx: UnsafeMutableRawPointer?, ch: UnsafePointer<xmlChar>?, len: CInt) {
let parser = Unmanaged<LibXMLParser>.fromOpaque(ctx!).takeUnretainedValue()
parser.delegate?.parserFoundCharacters(String(cString: ch!))
}
我用一个
URL
初始化这个类。当我调用 parse()
方法时,它会创建一个 URLSession
和一个 URLSessionDataTask
,并将代理设置为自身以覆盖方法 didReceive data: Data
。
然后我创建了一个 xmlParserCtxtPtr
并循环直到数据任务完成。
当它接收到数据时,我使用 xmlParseChunk
方法进行解析,startElementSAX
调用了我从 ViewController 类设置的委托。(我只需要元素名称、属性数量和属性。)
到目前为止都很好。
在我的 ViewController(UITableViewController)中,我有以下代码:
func downloadBooksLibXML() {
print("Downloading…")
UIApplication.shared.isNetworkActivityIndicatorVisible = true
DispatchQueue.global().async {
print("Setting up parser")
let parser = LibXMLParser(url: URL(string: self.baseUrl + self.parameters!)!)
parser.delegate = self
parser.parse()
}
}
func parserDidStartDocument() {
}
func parserDidEndDocument() {
DispatchQueue.main.sync {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
self.isDone = true
print("Finished")
}
}
func parserDidStartElement(_ elementName: String, nb_attributes: CInt, attributes: UnsafeMutablePointer<UnsafePointer<xmlChar>?>?) {
print(elementName)
switch elementName {
case "Book":
DispatchQueue.main.async {
let book = self.buildBook(nb_attributes: nb_attributes, attributes: attributes)
self.books.append(book)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [IndexPath(row: self.books.count - 1, section: 0)], with: .automatic)
self.tableView.endUpdates()
self.navigationItem.title = String(format: NSLocalizedString("books_found", comment: "Books found"), "\(self.books.count)")
}
case "ResultList":
break
case "ResultInfo":
break
default:
break
}
}
func buildBook(nb_attributes: CInt, attributes: UnsafeMutablePointer<UnsafePointer<xmlChar>?>?) -> Book {
let fields = 5 /* (localname/prefix/URI/value/end) */
let book = Book()
for i in 0..<Int(nb_attributes) {
if let localname = attributes?[i * fields + 0],
//let prefix = attributes?[i * fields + 1],
//let URI = attributes?[i * fields + 2],
let value_start = attributes?[i * fields + 3],
let value_end = attributes?[i * fields + 4] {
let localnameString = String(cString: localname)
let string_start = String(cString: value_start)
let string_end = String(cString: value_end)
let diff = string_start.characters.count - string_end.characters.count
if diff > 0 {
let value = string_start.substring(to: string_start.index(string_start.startIndex, offsetBy: diff))
book.setValue(value, forKey: localnameString)
}
}
}
return book
}
func parserDidEndElement(_ elementName: String) {
}
func parserFoundCharacters(_ string: String) {
}
func parserErrorOccurred(_ parseError: Error?) {
}
------
更新
nwellnhof的答案解决了获取属性值的问题。我已经更新了代码,现在更好了。它不再遍历所有属性。 现在我的新问题是:
我创建了buildBook
方法来获取XML属性的Book
对象。我大多数是从这里翻译过来的:What is the right way to get attribute value in libXML sax parser (C++)? 然后使用setValue(value: Any?, forKey: String)
设置我的书籍对象的属性。
但现在我的问题是它没有更新tableView。我尝试在后台线程中同步执行buildBook
方法,并在主线程中异步更新tableView,使用DispatchQueue.global().sync
和DispatchQueue.main.async
。但是,尽管它在主线程中,它仍然在tableView.endUpdates()
崩溃。
------
非常感谢任何帮助。