조회 수 211 추천 수 0 댓글 0
Atachment
첨부 '1'
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
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

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
179 iOS Swift로 개발한 Flappy Bird 오픈소스 - FlappySwift file hooni 2014.07.01 448
178 iOS Swift로 개발한 캔디 크러쉬 사가 - CookieCrunch file hooni 2014.10.06 497
177 iOS Swipe to Delete and the “More” button (like in Mail app on iOS 7) file hooni 2016.04.19 649
176 iOS switch 문 자동완성 Xcode 플러그인 - SCXcodeSwitchExpander file hooni 2014.07.01 275
175 iOS TinyWings 같은 게임만들기 -TinySeal file hooni 2014.03.18 290
174 iOS TODO 관리를 쉽게해주는 Xcode 플러그인 - XToDo file hooni 2014.03.19 247
173 iOS UIBezierPath만을 이용해 공유버튼 만들기 - GRButtons file hooni 2014.03.18 252
172 iOS UICollectionView 기반 채팅 UI 구현 오픈소스, SPHChatCollectionView file hooni 2015.07.20 495
171 iOS UICollectionView의 다양한 커스텀 레이아웃지원 예제 - IntroducingCollectionViews file hooni 2014.03.19 295
170 iOS UIScrollView + 페이지접힘효과 = PaperFoldGallery file hooni 2014.03.18 289
169 iOS UITableView + UIAlertView = SBTableAlert file hooni 2014.03.18 219
168 iOS UITableView 를 커스터마이징한 예제입니다. file hooni 2014.03.18 233
167 iOS UIWebView에서 한글 txt 파일 정상적으로 표시 file hooni 2014.03.18 277
166 iOS Xcode 플러그인 ColorSense file hooni 2014.03.18 277
165 iOS Xcode플러그인관리 플러그인 - JDListInstalledPlugins file hooni 2014.03.18 202
» iOS XML 파싱을 쉽게 해주는 TBXML file hooni 2014.03.18 211
Board Pagination Prev 1 2 3 4 5 6 ... 15 Next
/ 15