iOS
2014.03.18 15:23
XML 파싱을 쉽게 해주는 TBXML
조회 수 214 추천 수 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
228 | iOS | 현재위치와 간단텍스트 저장하는 예제 | hooni | 2014.03.18 | 284 |
227 | iOS | 해커뉴스앱 오픈소스 | hooni | 2014.03.19 | 383 |
226 | iOS | 플립보드 화면전환효과 구현하기 - FlipBoardNavigationController | hooni | 2014.03.18 | 272 |
225 | iOS | 플립보드 스타일 오픈소스 - FlipView | hooni | 2014.03.18 | 238 |
224 | iOS | 플로팅 스타일 네비게이션 오픈소스, JVFloatingDrawer | hooni | 2015.07.20 | 571 |
223 | iOS | 플랫 UI 커스텀 컨트롤 - FlatUIKit | hooni | 2014.03.18 | 248 |
222 | iOS | 프로젝트에 추가된 이미지명 자동완성 Xcode 플러그인 - KSImageNamed-Xcode | hooni | 2014.07.01 | 305 |
221 | iOS | 프로그래스바관련 오픈소스 - MBProgressHUD | hooni | 2014.03.18 | 310 |
220 | iOS | 풀스크린모달뷰 - MZFormSheetController | hooni | 2014.03.19 | 324 |
219 | iOS | 풀다운UI 컨트롤 - MBPullDownController | hooni | 2014.03.18 | 356 |
218 | iOS | 폼형식 입력가능한 ELCTextFieldCell 예제 | hooni | 2014.03.18 | 295 |
217 | iOS | 폴딩,플립 화면전환 오픈소스 - MPFoldTransition | hooni | 2014.03.18 | 301 |