iOS
2014.03.18 15:23
XML 파싱을 쉽게 해주는 TBXML
조회 수 211 추천 수 0 댓글 0
첨부 '1' |
---|
XML 파싱할때 직관적으로 코딩할 수 있는 TBXML을 소개합니다.
아래는 예제파일에 포함된 소스코드 일부분입니다. 대략 이런식으로 코딩이 가능합니다. 참고하세요. ^^
// instantiate an array to hold author objects authors = [[NSMutableArray alloc] initWithCapacity:10]; // Load and parse the books.xml file tbxml = [[TBXML tbxmlWithXMLFile:@"books.xml"] retain]; // Obtain root element TBXMLElement * root = tbxml.rootXMLElement; // if root element is valid if (root) { // search for the first author element within the root element's children TBXMLElement * author = [TBXML childElementNamed:@"author" parentElement:root]; // if an author element was found while (author != nil) { // instantiate an author object Author * anAuthor = [[Author alloc] init]; // get the name attribute from the author element anAuthor.name = [TBXML valueOfAttributeNamed:@"name" forElement:author]; // search the author's child elements for a book element TBXMLElement * book = [TBXML childElementNamed:@"book" parentElement:author]; // if a book element was found while (book != nil) { // instantiate a book object Book * aBook = [[Book alloc] init]; // extract the title attribute from the book element aBook.title = [TBXML valueOfAttributeNamed:@"title" forElement:book]; // extract the title attribute from the book element NSString * price = [TBXML valueOfAttributeNamed:@"price" forElement:book]; // if we found a price if (price != nil) { // obtain the price from the book element aBook.price = [NSNumber numberWithFloat:[price floatValue]]; } // find the description child element of the book element TBXMLElement * desc = [TBXML childElementNamed:@"description" parentElement:book]; // if we found a description if (desc != nil) { // obtain the text from the description element aBook.description = [TBXML textForElement:desc]; } // add the book object to the author's books array and release the resource [anAuthor.books addObject:aBook]; [aBook release]; // find the next sibling element named "book" book = [TBXML nextSiblingNamed:@"book" searchFromElement:book]; } // add our author object to the authors array and release the resource [authors addObject:anAuthor]; [anAuthor release]; // find the next sibling element named "author" author = [TBXML nextSiblingNamed:@"author" searchFromElement:author]; } } // release resources [tbxml release];
소스다운로드는 첨부파일 또는 아래링크를 참고하세요.
http://www.tbxml.co.uk/TBXML/TBXML_Free.html
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
36 | iOS |
커스텀 타이머 컨트롤 - DDHTimerControl
![]() |
hooni | 2014.10.06 | 337 |
35 | iOS |
아이패드 3D 모델링 앱 오픈소스 - Revolved
![]() |
hooni | 2014.10.06 | 681 |
34 | iOS |
코어 데이터 편집기 오픈 소스 - Core Data Editor
![]() |
hooni | 2014.10.06 | 331 |
33 | iOS |
커스텀 달력 오픈소스 - RSDayFlow
![]() |
hooni | 2014.10.06 | 425 |
32 | iOS |
스와이프 제스처 기반 카드 컨트롤 오픈소스 - TinderSimpleSwipeCards
![]() |
hooni | 2014.10.06 | 405 |
31 | iOS |
앱 사용 가이드 템플릿 오픈소스 - BWWalkthrough
![]() |
hooni | 2014.10.06 | 317 |
30 | iOS |
동영상 녹화시 유용한 터치 제스츄어 표시 오픈소스 - Fingertips
![]() |
hooni | 2014.10.06 | 318 |
29 | iOS |
애플워치의 UI구현 오픈소스, WatchSpringboard
![]() |
hooni | 2014.11.28 | 385 |
28 | iOS |
커스텀 Pull-To-Refressh 구현 오픈소스 - CBStoreHouseRefreshControl
![]() |
hooni | 2014.11.28 | 309 |
27 | iOS |
스와이프 제스처 기반 카드 컨트롤 오픈소스 - ZLSwipeableView
![]() |
hooni | 2014.11.28 | 385 |
26 | iOS |
JSON 데이터를 스위프트 코드로 자동 변환해주는 맥용 오픈소스 - JSONExport
![]() |
hooni | 2014.11.28 | 313 |
25 | iOS |
[ios] 애니메이션 초간단 예제
![]() |
hooni | 2015.01.16 | 342 |