SWXMLHash 项目简介
SWXMLHash 是相对简单的使用 Swift 解析 XML 的方式,如果想拥有类似 NSXMLParser 的功能,SWXMLHash 会提供一个简单的封装,同时提供一个 XML 转换成数组目录的方式(比如 hash)。API 灵感来源于 SwiftyJSON。示例:// instantiate your SWXMLHash instance
let parser = SWXMLHash()
// begin parsing
let xml = parser.parse(xmlToParse)
// will return "Test Title Header"
xml["root"]["header"]["title"].element?.text
// will return "Ralls, Kim"
xml["root"]["catalog"]["book"][1]["author"].element?.text
// will return "bk102"
xml["root"]["catalog"]["book"][1].element?.attributes["id"]
// will return "Computer, Fantasy, Fantasy
"", ".join(xml["root"]["catalog"]["book"].all.map { elem in elem["genre"].element!.text! })
// error handling
switch
xml["root"]["what"]["header"]["foo"] {
case .Element(let elem):
// everything is good, code away!
case .Error(let error):
// error is an NSError instance that you can deal with}
let parser = SWXMLHash()
// begin parsing
let xml = parser.parse(xmlToParse)
// will return "Test Title Header"
xml["root"]["header"]["title"].element?.text
// will return "Ralls, Kim"
xml["root"]["catalog"]["book"][1]["author"].element?.text
// will return "bk102"
xml["root"]["catalog"]["book"][1].element?.attributes["id"]
// will return "Computer, Fantasy, Fantasy
"", ".join(xml["root"]["catalog"]["book"].all.map { elem in elem["genre"].element!.text! })
// error handling
switch
xml["root"]["what"]["header"]["foo"] {
case .Element(let elem):
// everything is good, code away!
case .Error(let error):
// error is an NSError instance that you can deal with}