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
Who's hooni
![profile](https://www.hooni.net/xe/files/member_extra_info/profile_image/004/4.jpg?20220313112955)
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
168 | iOS |
Pull to Refresh + 탁구 = BOZPongRefreshControl
![]() |
hooni | 2014.03.19 | 229 |
167 | iOS |
오디오 프레임워크 - EZAudio
![]() |
hooni | 2014.03.19 | 213 |
166 | iOS |
사파리 호출 이벤트 가로채기 예제 - UITextViewLinkOptions
![]() |
hooni | 2014.03.19 | 221 |
165 | iOS |
TODO 관리를 쉽게해주는 Xcode 플러그인 - XToDo
![]() |
hooni | 2014.03.19 | 254 |
164 | iOS |
코드를 보기좋게 재정렬 해주는 Xcode 플러그인 - XAlign
![]() |
hooni | 2014.03.19 | 227 |
163 | iOS |
맥의 Dock을 iOS에서 구현하기 - MCSFishEye
![]() |
hooni | 2014.03.19 | 276 |
162 | iOS |
애니메이션기능이 추가된 막대, 라인 차트 - PNChart
![]() |
hooni | 2014.03.19 | 245 |
161 | iOS |
오픈소스 폰트 프레임워크 - ionicons
![]() |
hooni | 2014.03.19 | 237 |
160 | iOS |
커스텀 상태바 알림 - JDStatusBarNotification
![]() |
hooni | 2014.03.19 | 212 |
159 | iOS |
iOS 7 API를 활용한 샘플 소스 코드 - iOS7-Sampler
![]() |
hooni | 2014.03.19 | 216 |
158 | iOS |
스와이프 제스처 기반 커스텀 테이블뷰셀 II- slideActionCell
![]() |
hooni | 2014.03.19 | 238 |
157 | iOS |
Placeholder값을 텍스트필드의 타이틀로 바꿔주는 커스텀 컨트롤 - JVFloatLabeledTextField
![]() |
hooni | 2014.03.19 | 244 |