我现在正在处理复杂的 XML 解析。
这是链接:https ://www.reddit.com/hot/.rss
我使用 Alamofire 来获取数据:
protocol APIUsable {
}
结构 API:APIUsable {
static func fetchRedditFeedListData() -> Observable<[Entry]>{
let URL = "https://www.reddit.com/hot/.rss"
return Observable.create { observer in
Alamofire.request(URL).response { response in
guard let data = response.data else {
return
}
do {
let xml = SWXMLHash.parse(data)
let entries: [Entry] = try xml["feed"]["entry"].value()
observer.onNext(entries)
} catch let error as NSError {
print(error.localizedDescription)
}
observer.onCompleted()
}
return Disposables.create()
}
}
}
以下是我为解析而构建的结构。它运作良好。
struct Entry: XMLIndexerDeserializable {
let title: String
let updated: String
let category: String
let content: String
static func deserialize(_ node: XMLIndexer) throws -> Entry {
return try Entry(
title: node["title"].value(),
updated: node["updated"].value(),
category: node["category"].value(ofAttribute: "term"),
content: node["content"].value()
)
}
}
但是我也想获取图片字符串属于content,img,src