Swift學習筆記(2)網路資料交換格式(XML,JSON)解析 [iOS實戰 入門與提高卷]
Swift學習筆記(2)網路資料交換格式(XML,JSON)解析
參考書籍及資源:iOS實戰 入門與提高卷 關東昇 參考書籍地址
- 用NSXML來解析XML文件
- 用TBXML來解析XML文件
- 用NSJSONSerialization來解析JSON文件
目錄
用NSXML來解析XML文件
NSXML是iOS SDK自帶的,也是蘋果預設的解析框架,框架的核心是NSXMLParser和它的委託協議NSXMLParserDelegate。
示例文件Notes.xml
<?xml version="1.0" encoding="UTF-8"?>
<Notes>
<Note id="1">
<CDate>2014-12-21</CDate>
<Content>早上8點鐘到公司</Content>
<UserID>tony</UserID>
</Note>
<Note id="2">
<CDate>2014-12-22</CDate>
<Content>釋出iOSBook1</Content>
<UserID>tony</UserID>
</Note>
<Note id="3">
<CDate>2014-12-23</CDate>
<Content>釋出iOSBook2</Content>
<UserID>tony</UserID>
</Note>
<Note id="4">
<CDate>2014-12-24</CDate>
<Content>釋出iOSBook3</Content>
<UserID>tony</UserID>
</Note>
<Note id="5">
<CDate>2014-12-25</CDate>
<Content>釋出2016奧運會應用iPhone版本</Content>
<UserID>tony</UserID>
</Note>
<Note id="6">
<CDate>2014-12-26</CDate>
<Content>釋出2016奧運會應用iPad版本</Content>
<UserID>tony</UserID>
</Note>
</Notes>
建立XMLParser類
import Foundation
class XMLParser: NSObject , NSXMLParserDelegate {
private var notes:NSMutableArray! = []
private var currentTagName:String!
func startParse(){
NSLog("start parse")
let path=NSBundle.mainBundle().pathForResource("Notes", ofType: "xml")!
let url=NSURL(fileURLWithPath: path)
//開始解析
let parser=NSXMLParser(contentsOfURL: url)!
parser.delegate=self
parser.parse()
}
//文件開始時觸發
func parserDidStartDocument(parser: NSXMLParser) {
self.notes=NSMutableArray()
}
//文件出錯時觸發
func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
NSLog("%@", parseError)
}
//遇到一個開始標籤時觸發,其中namespaceURI是名稱空間,qualifiedName是限定名,attributes是字典屬性集合
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
self.currentTagName=elementName
if self.currentTagName == "Note"{
let id=attributeDict["id"]! as NSString
let dict=NSMutableDictionary()
dict.setObject(id, forKey: "id")
self.notes.addObject(dict)
}
}
//遇到字串時觸發
func parser(parser: NSXMLParser, foundCharacters string: String) {
//去除空格和回車
let s1 = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if s1 == ""{
return
}
let dict = self.notes.lastObject as! NSMutableDictionary
if (self.currentTagName == "CDate"){
dict.setObject(string, forKey: "CDate")
}
if (self.currentTagName == "Content"){
dict.setObject(string, forKey: "Content")
}
if (self.currentTagName == "UserID"){
dict.setObject(string, forKey: "UserID")
}
}
//遇到結束標籤時觸發
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
self.currentTagName=nil
}
//文件結束時觸發
func parserDidEndDocument(parser: NSXMLParser) {
NSLog("end parse")
NSLog("\(notes)")
}
}
呼叫與執行結果
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let parser=XMLParser()
parser.startParse()
}
2016-05-17 12:03:42.836 XMLTest[61377:445073] start parse
2016-05-17 12:03:42.852 XMLTest[61377:445073] end parse
2016-05-17 12:03:42.853 XMLTest[61377:445073] (
{
CDate = "2014-12-21";
Content = "\U65e9\U4e0a8\U70b9\U949f\U5230\U516c\U53f8";
UserID = tony;
id = 1;
},
{
CDate = "2014-12-22";
Content = "\U53d1\U5e03iOSBook1";
UserID = tony;
id = 2;
},
{
CDate = "2014-12-23";
Content = "\U53d1\U5e03iOSBook2";
UserID = tony;
id = 3;
},
{
CDate = "2014-12-24";
Content = "\U53d1\U5e03iOSBook3";
UserID = tony;
id = 4;
},
{
CDate = "2014-12-25";
Content = "\U53d1\U5e032016\U5965\U8fd0\U4f1a\U5e94\U7528iPhone\U7248\U672c";
UserID = tony;
id = 5;
},
{
CDate = "2014-12-26";
Content = "\U53d1\U5e032016\U5965\U8fd0\U4f1a\U5e94\U7528iPad\U7248\U672c";
UserID = tony;
id = 6;
}
)
用TBXML來解析XML文件
TBXML是第三方框架,使用起來比NSXML更簡單。
準備工作
下載完成後將TBXML-Headers和TBXML-Code資料夾新增到工程中,並新增以下Framewok和庫
在Xcode6以後的版本,需要建立PrefixHeader.pch檔案
並選擇TARGETS->工程名->Buil Setting->Apple LLVM x.x Language ->Prefix Header,輸入PrefixHeader.pch
在PrefixHeader.pch中新增以下程式碼
#import <Foundation/Foundation.h>
#define ARC_ENABLED
在橋接標頭檔案中新增以下程式碼(關於橋接標頭檔案請參考Swift和Objective-C的混編)
#import <Foundation/Foundation.h>
#import "TBXML.h"
建立XMLParser類
import Foundation
class XMLParser: NSObject {
private var notes:NSMutableArray! = []
func startParse(){
NSLog("start parse")
self.notes=NSMutableArray()
let tbxml=(try? TBXML(XMLFile: "Notes.xml",error:()))!
//獲取XML文件根元素
let root=tbxml.rootXMLElement
if root != nil{
//查詢root元素下的Note元素
var noteElement=TBXML.childElementNamed("Note", parentElement: root)
while noteElement != nil{
let dict=NSMutableDictionary()
//查詢Note元素下的CDate元素
let CDateElemet=TBXML.childElementNamed("CDate", parentElement: noteElement)
if CDateElemet != nil{
let CDate=TBXML.textForElement(CDateElemet)
dict.setValue(CDate, forKey: "CDate")
}
//查詢Note元素下的Content元素
let ContentElemet=TBXML.childElementNamed("Content", parentElement: noteElement)
if ContentElemet != nil{
let Content=TBXML.textForElement(ContentElemet)
dict.setValue(Content, forKey: "Content")
}
//查詢Note元素下的UserID元素
let UserIDElemet=TBXML.childElementNamed("UserID", parentElement: noteElement)
if UserIDElemet != nil{
let UserID=TBXML.textForElement(UserIDElemet)
dict.setValue(UserID, forKey: "UserID")
}
//獲取Note元素的id屬性值
let id=TBXML.valueOfAttributeNamed("id", forElement: noteElement)
dict.setValue(id, forKey: "id")
self.notes.addObject(dict)
//獲取同層的下一個Note元素
noteElement=TBXML.nextSiblingNamed("Note", searchFromElement: noteElement)
}
}
NSLog("end parse")
NSLog("\(notes)")
self.notes=nil
}
}
呼叫與執行結果
同上
用NSJSONSerialization來解析JSON文件
NSJSONSerialization是iOS 5之後蘋果提供的API。
示例文件 Notes.data
{"ResultCode":0,"Record":[
{"ID":"1","CDate":"2014-12-23","Content":"釋出iOSBook0","UserID":"tony"},
{"ID":"2","CDate":"2014-12-24","Content":"釋出iOSBook1","UserID":"tony"},
{"ID":"3","CDate":"2014-12-25","Content":"釋出iOSBook2","UserID":"tony"},
{"ID":"4","CDate":"2014-12-26","Content":"釋出iOSBook3","UserID":"tony"},
{"ID":"5","CDate":"2014-12-27","Content":"釋出iOSBook4","UserID":"tony"},
{"ID":"6","CDate":"2014-12-28","Content":"釋出iOSBook5","UserID":"tony"},
{"ID":"7","CDate":"2014-12-29","Content":"釋出iOSBook6","UserID":"tony"},
{"ID":"8","CDate":"2014-12-30","Content":"釋出iOSBook7","UserID":"tony"},
{"ID":"9","CDate":"2014-12-31","Content":"釋出iOSBook8","UserID":"tony"},
{"ID":"10","CDate":"2014-12-32","Content":"釋出iOSBook9","UserID":"tony"},
{"ID":"11","CDate":"2014-12-33","Content":"釋出iOSBook10","UserID":"tony"},
{"ID":"12","CDate":"2014-12-34","Content":"釋出iOSBook11","UserID":"tony"},
{"ID":"13","CDate":"2014-12-35","Content":"釋出iOSBook12","UserID":"tony"},
{"ID":"14","CDate":"2014-12-36","Content":"釋出iOSBook13","UserID":"tony"},
{"ID":"15","CDate":"2014-12-37","Content":"釋出iOSBook14","UserID":"tony"},
{"ID":"16","CDate":"2014-12-38","Content":"釋出iOSBook15","UserID":"tony"},
{"ID":"17","CDate":"2014-12-39","Content":"釋出iOSBook16","UserID":"tony"},
{"ID":"18","CDate":"2014-12-40","Content":"釋出iOSBook17","UserID":"tony"},
{"ID":"19","CDate":"2014-12-41","Content":"釋出iOSBook18","UserID":"tony"},
{"ID":"20","CDate":"2014-12-42","Content":"釋出iOSBook19","UserID":"tony"},
{"ID":"21","CDate":"2014-12-43","Content":"釋出iOSBook20","UserID":"tony"},
{"ID":"22","CDate":"2014-12-44","Content":"釋出iOSBook21","UserID":"tony"}]}
示例程式碼
import UIKit
class ViewController: UIViewController {
var objects:NSMutableArray!=[]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path=NSBundle.mainBundle().pathForResource("Notes", ofType: "json")!
let jsonData=NSData(contentsOfFile: path)!
//MutableContainers指定解析返回的是可變的陣列或字典
let jsonObj:NSDictionary=(try? NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
self.objects=jsonObj.objectForKey("Record") as! NSMutableArray
NSLog("\(self.objects)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
執行結果
略
相關文章
- 深度學習入門筆記(十八):卷積神經網路(一)深度學習筆記卷積神經網路
- go實戰web入門學習筆記GoWeb筆記
- 資料格式之戰:JSON vs XMLJSONXML
- Swift學習筆記(3)iOS 9 中的網路請求Swift筆記iOS
- 《Python入門與資料科學庫》學習筆記Python資料科學筆記
- iOS開發之解析XML格式資料iOSXML
- 【Go學習筆記16】解析xmlGo筆記XML
- swift學習筆記《2》-swift語法Swift筆記
- JSON筆記,用於資料交換JSON筆記
- 深度學習筆記------卷積神經網路深度學習筆記卷積神經網路
- 深度學習卷積神經網路筆記深度學習卷積神經網路筆記
- 電商大資料學習筆記:實戰大資料筆記
- (五)numpy知識學習2-python資料分析與機器學習實戰(學習筆記)Python機器學習筆記
- 學習筆記(1):Python零基礎入門到實戰-資料科學原理與資料處理流程筆記Python資料科學
- iOS學習筆記39 ReactiveCocoa入門iOS筆記React
- Swift iOS : 解析jsonSwiftiOSJSON
- 快速入門——深度學習理論解析與實戰應用深度學習
- Docker學習筆記:映象、容器、資料卷Docker筆記
- iOS學習筆記43 Swift(三)類iOS筆記Swift
- 卷積神經網路學習筆記——SENet卷積神經網路筆記SENet
- substrate學習筆記2:substrate快速入門筆記
- Mysql學習筆記2--使用入門MySql筆記
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS
- JAVA中 XML與資料庫互轉 學習筆記三JavaXML資料庫筆記
- iOS學習筆記14 網路(三)WebViewiOS筆記WebView
- 學習筆記|AS入門(七) 資料儲存篇筆記
- 無線感測器網路與資料交換解析
- 卷積神經網路學習資料卷積神經網路
- JSON資料交換格式有幾種?JSON
- 《Java核心技術 卷I》學習筆記2:資料型別、變數與常量Java筆記資料型別變數
- Swift快速入門筆記Swift筆記
- iOS學習筆記47 Swift(七)泛型iOS筆記Swift泛型
- IOS 學習之XML解析(一)iOSXML
- swift學習筆記《5》- 實用Swift筆記
- AS 學習筆記 載入資料筆記
- 語音學習筆記12------直觀理解深度學習與卷積神經網路筆記深度學習卷積神經網路
- XML初學進階學習筆記(2)(轉)XML筆記
- TS入門學習筆記筆記