XML解析之SAX詳解

来源:http://www.cnblogs.com/ldnh/archive/2016/03/19/5294605.html
-Advertisement-
Play Games

XML解析之SAX詳解 本文屬於作者原創 http://www.cnblogs.com/ldnh/ XML解析的五個步驟 1、打開文檔 (void)parserDidStartDocument:(NSXMLParser )parser ; 2、開始查找起始標簽 (void)parser:(NSXML


XML解析之SAX詳解

本文屬於作者原創 http://www.cnblogs.com/ldnh/

XML解析的五個步驟

  • 1、打開文檔

  • (void)parserDidStartDocument:(NSXMLParser *)parser ;

  • 2、開始查找起始標簽

  • (void)parser:(NSXMLParser )parser didStartElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName attributes:(NSDictionary *)attributeDict;

  • 3、獲取標簽內容

  • (void)parser:(NSXMLParser )parser foundCharacters:(NSString )string

  • 4、查找結束標簽

  • -(void)parser:(NSXMLParser )parser didEndElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName

  • 5、查詢文檔結束

  • (void)parserDidEndDocument:(NSXMLParser *)parser {

解析詳細代碼

#import "ViewController.h"
#import "JKPTrainInfo.h"
@interface ViewController ()<UITableViewDataSource,NSXMLParserDelegate>

@property (nonatomic,strong) NSMutableArray *dataList;

@property (nonatomic,strong) UITableView *tableView;

//因為下麵多次要用,所以要建一個全局變數
@property (nonatomic,strong) JKPTrainInfo *currentInfo;

@property (nonatomic,strong) NSMutableString *muString;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//載入視圖
[self initUI];
//載入數據
[self loadData];

}

#pragma mark -getter and setter methods

-(NSMutableArray *)dataList
{
if (!_dataList) {
  
  _dataList = [NSMutableArray array];
}
return _dataList;
}

-(UITableView *)tableView
{
if (!_tableView) {
  
  _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
  
  _tableView.dataSource = self;
}
return _tableView;
}


- (NSMutableString *)muString
{
if (!_muString) {
  _muString = [NSMutableString string];
}

return _muString;
}

#pragma mark - UITableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString * identifiter = @"cellID";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifiter];

if (!cell) {
  
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifiter];
}
JKPTrainInfo *JkpInfo = self.dataList[indexPath.row];

cell.textLabel.text = JkpInfo.trainStation;

cell.detailTextLabel.text = JkpInfo.arriveTime;

return cell;
}



#pragma mark - NSXMLParserDelegate 

//1. 打開文件,開始解析
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"打開文件,開始解析");

//每次解析前進行清空操作-防止多次載入重覆數據

[self.dataList removeAllObjects];
}

//2. 查找起始標簽
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{


//    NSLog(@"%@",elementName);
//    
//    NSLog(@"%@",attributeDict);

if ([elementName isEqualToString:@"trainDetailInfo"]) {
  
  self.currentInfo = [[JKPTrainInfo alloc]init];
  
  [self.currentInfo setValuesForKeysWithDictionary:attributeDict];
}

//清空muString 數據
self.muString.string = @"";
}

//3.獲取標簽內容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//    NSLog(@"%@",string);

//防止多次執行獲取內容的方法
[self.muString appendString:string];
}

// 4.查找結束標簽
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//    NSLog(@"%@",elementName);

if ([elementName isEqualToString:@"trainDetailInfo"]) {
  
  [self.dataList addObject:self.currentInfo];
}

else if (![elementName isEqualToString:@"dataSet"]&&![elementName isEqualToString:@"diffgr:diffgram"])
{
  // self.muString是數據內容
  [self.currentInfo setValue:self.muString forKey:elementName];
}

}

//5.解析完成
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"解析完成");

//解析完成後刷新數據
[self.tableView reloadData];
}

#pragma mark - event handle methods

- (void)loadData
{
NSString *urlString = @"http://192.168.1.68/train.xml";

NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *requst = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  
  //XML解析
  NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
  
  parser.delegate = self;
  
  [parser parse];
  
}];

}

- (void)initUI
{
[self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 問題 在ios 9中使用HTTP請求,Xcode就會拋出下麵的Exception: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary
  • 管理範圍: 管理任何繼承NSObject的對象,對其他的基本數據類型無 效 本質原因是因為對象和其他數據類型在系統中的存儲空間不一樣,其它局部變數主要存放於 棧中,而對象存儲於堆中,當代碼塊結束時這個代碼塊中涉及的所有局部變數會被回收,指 向對象的指針也被回收,此時對象已經沒有指針指向,但依然存在於
  • 或許我沒見過世面,但是這卻是我看過對“怎麼成為一個優秀的程式員,而不是一個優秀的碼農?”最精彩的回答【全部複製粘貼】 其實我的心一直因為現在的工作【餐飲服務】而動搖,忽而想學網站,忽而c語言根基,忽而c#忽而java Android方向,然而當讀到這個問答時,我堅定我所喜歡的iOS,不管結局如何(一
  • XML解析(DOM) 1. 開始解析某個元素,會遍歷整個XML,識別元素節點名稱 - (void)parser:didStartElement:namespaceURI:qualifiedName:attributes: 2. 文本節點,得到文本節點里存儲的信息數據,對於大數據可能會接收多次!為了節
  • UIview內部有個預設的CALayer對象層,雖然我門不可以重新創建它,但是我門可以再其上面添加子層。 我們知道,UIView有 addSubview:方法,同樣,CALayer也有addSublayer:方法。我門可以通過addSublayer:再已有layer上添加自層。 下麵我門演示如何添加
  • Android平臺給我們提供了一個SharedPreferences類,實際上SharedPreferences處理的就是一個key-value(鍵值對),它是 一個輕量級的存儲類,特別適合用於保存軟體配置參數及用戶的偏好設置參數,比如登錄時候的記住密碼功能等。使用 SharedPreference
  • 對於一個app的好壞,我們首要判斷的便是app的界面,而界面的建立則是在圖形的處理基礎上的,說到圖形處理又不得不提及Quartz2D,CALayer。 在iOS系統中,你能看得見摸得著的東西基本上都是UIView,比如一個按鈕、一個文本標簽、一個文本輸入框、一個圖標等等,這些都是UIView。 其實
  • 兩個NSDate之間是可以進行比較的。 有了NSCalendar比較兩個日期就易如反掌 /** 1.今年 1> 今天 * 1分內: 剛剛 * 1分~59分內:xx分鐘前 * 大於60分鐘:xx小時前 2> 昨天 * 昨天 xx:xx 3> 其他 * xx-xx xx:xx 2.非今年 1> xxxx
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...