Created
June 23, 2013 01:35
-
-
Save joohee/5843380 to your computer and use it in GitHub Desktop.
JSON, XML, Html Parse of iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JSON example | |
// - receiedData : {"feed" : {"title" : "my title", "content" : "my content"}} 인 경우 | |
- (void) startJsonParse(NSData *receivedData) | |
{ | |
NSError* error; | |
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptionserror:&error]; | |
NSArray *array = [[dictionary objectForKey:@"feed"] objectForKey:@"entry"]; | |
for (NSDictionary *entry in array) { | |
NSString *title = [[entry objectForKey:@"title"] valueForKey:@"title"]; | |
NSString *content = [[entry objectForKey:@"link"] valueForKey:@"href"]; | |
NSLog(@"%@, %@", title, content); // my title, my content | |
} | |
} | |
// XML example | |
// - interface 에 xml parser delegate 선언 및 NSXmlParser 멤버 변수 추가 | |
@interface Crawler <NSXMLParserDelegate> { | |
NSXMLParser *_xmlParser; | |
NSMutableString *_currentXmlValue; | |
… | |
} | |
//- 초기화 및 delegate를 나 자신으로 지정 | |
- (void) init | |
{ | |
… | |
_xmlParser = [[NSXMLParseralloc] init]; | |
_xmlParser.delegate = self; | |
} | |
//- parse 시작 | |
- (void) startXmlParse(NSData *receivedData) | |
{ | |
bool isParse = [_xmlParserinitWithData:receivedData]; | |
if (isParse) | |
[_xmlParser parse]; | |
} | |
// - delegate에 선언된 함수들 구현 | |
#pragma mark - xml parsing | |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName | |
namespaceURI:(NSString *)namespaceURI | |
qualifiedName:(NSString *)qName | |
attributes:(NSDictionary *)attributeDict | |
{ | |
// <item> 태그 발견 시 파싱 준비 | |
if ([elementName isEqualToString:@"item"]) { | |
_xmlDataDictionary = [[NSMutableDictionaryalloc] init]; | |
if (!_currentXmlValue) | |
_currentXmlValue = [[NSMutableStringalloc] init]; | |
else | |
[_currentXmlValue setString:@""]; | |
} | |
} | |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string | |
{ | |
[_currentXmlValue appendString:string]; | |
} | |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName | |
namespaceURI:(NSString *)namespaceURI | |
qualifiedName:(NSString *)qName | |
{ | |
if ([elementName isEqualToString:@"item"]) { | |
NSString *title = [_xmlDataDictionary valueForKey:@"title"]; | |
NSString *content = [_xmlDataDictionary valueForKey:@"description"]; | |
NSLog(@"%@, %@", title, content); | |
} else { | |
if (_currentXmlValue) { | |
[_xmlDataDictionary setValue:[NSStringstringWithString:_currentXmlValue] forKey:elementName]; | |
[_currentXmlValue setString:@""]; | |
} | |
} | |
} | |
HTML Parsing using THPPLE | |
- http://m.search.daum.net/search?w=news&q=%EB%A5%98%ED%98%84%EC%A7%84&managed_type=recency&spacing=0 의 thumbnail, 제목, 요약 파싱 | |
- (void) startHtmlParse(NSData *receivedData) | |
{ | |
TFHpple *daumParser = [TFHpple hppleWithHTMLData:receivedData]; | |
NSString *daumXPathQueryStringForThumb = @"//*[@class='list_info']/li"; | |
NSArray *daumNewNodes = [daumParser searchWithXPathQuery:daumXPathQueryStringForThumb]; | |
for (TFHppleElement *daumElement in daumNewNodes) { | |
RyuNewsObject *news = [[RyuNewsObjectalloc] init]; | |
TFHppleElement *thumbElem = [[[daumElement firstChildWithTagName:@"a" ] | |
firstChildWithClassName:@"mg_cont"] | |
firstChildWithClassName:@"wrap_thumb"]; | |
if (thumbElem) { | |
NSString *thumbnailUrl = [[[thumbElem firstChildWithClassName:@"thumb"] firstChildWithTagName:@"img"] objectForKey:@"src"]; | |
NSLog(@"%@", thumbnailUrl); | |
} | |
TFHppleElement *contentElem = [[[[daumElement firstChildWithTagName:@"a"] | |
firstChildWithClassName:@"mg_cont"] | |
firstChildWithClassName:@"wrap_cont"] | |
firstChildWithClassName:@"cont"]; | |
if (contentElem) { | |
NSString *title = [[[contentElem firstChildWithClassName:@"wrap_tit"] text] stripHtml]; | |
NSString *content = [[[contentElem firstChildWithClassName:@"desc f_db"] text] stripHtml]; | |
NSLog(@"%@, %@", titile, content); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment