0

I am now working on a complex XML parsing.

Here is the link: https://www.reddit.com/hot/.rss

I use Alamofire to fetch data:

protocol APIUsable {

}

struct 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()
    }
}

}

The following is the struct I build for parsing. And It works well.

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()
    )
}

}

But I also want to get the image string belong to content, img, src

braaterAfrikaaner
  • 1,072
  • 10
  • 20
BeauZ
  • 1
  • 1

1 Answers1

0

I have found the solution by myself.

As we can see that there is a HTML in a XML.

So, I use SWXMLHash again to parse content

let xml = SWXMLHash.parse(/*put the content String here*/)
let imageString = xml ["table"]["tr"]["td"][0]["a"]["img"].element?.attribute(by: "src")?.text

then we can get the value of src as string for future use

BeauZ
  • 1
  • 1