Contents

조회 수 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
번호 분류 제목 글쓴이 날짜 조회 수
228 iOS 3D 차트 오픈소스 FRD3DBarChart file hooni 2014.03.18 274
227 iOS A SpaceManager Game - 앵그리버드와 같은방식의 게임소스 file hooni 2014.03.18 230
226 iOS Accelerometer를 활용한 Cocos2D게임 - Sapus Tongue file hooni 2014.03.18 209
225 iOS AV Foundation 데모 앱 - AVFoundationDemos file hooni 2014.03.18 237
224 iOS Block기반 UIAlertView 오픈소스 - AHAlertView file hooni 2014.03.18 203
223 iOS Cell 높이를 동적으로 조절하는 예제 file hooni 2014.03.18 249
222 iOS Chute Photo Picker+ file hooni 2014.03.18 197
221 iOS Circle Progress View - GSProgressView file hooni 2014.03.18 220
220 iOS climbers - 바위를 피해 벽을 오르는 게임 file hooni 2014.03.18 217
219 iOS Cocos2D를 이용한 전자책 개발 오픈소스 - idiary file hooni 2014.03.18 210
218 iOS Cocos2D를 이용한 파티클 엔진 예제 file hooni 2014.03.18 234
217 iOS DCRoundSwitch - 커스텀 스위치버튼 file hooni 2014.03.18 214
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 19 Next
/ 19