2011-11-21 3 views
0

선택적 구문 분석을 수행하는 나는 다음과 같은 XML을 가지고어떻게 NSXMLParser

<data> 
<title>Bookstore</title> 
<site>www.bookstore.com</site> 
<lastUpdate>11/17/2011</lastUpdate> 
<books> 
<unit> 
<title> 
Beginning iPhone 4 Development: Exploring the iOS SDK 
</title> 
<author>David Mark, Jack Nutting, Jeff LaMarche</author> 
<isbn10>143023024X</isbn10> 
<isbn13>978-1430230243</isbn13> 
<pubDate>January 28, 2011</pubDate> 
<price>23.99</price> 
<description> 
Beginning iPhone 4 Development is a complete course in iOS development. You'll master techniques that work on iPhone, iPad, and iPod touch. We start with the basics showing you how to download and install the tools you'll need, and how to create your first simple application. Next you'll learn to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders. 
</description> 
</unit> 
<unit>...</unit> 
<unit>...</unit> 
</books> 
<clothes> 
<unit> 
<title>T-Shirt</title> 
<size>M</size> 
<color>Red</color> 
<price>10.99</price> 
<description> 
100% cotton T-shirt, wash in cold water with like colors 
</description> 
</unit> 
<unit>...</unit> 
<unit>...</unit> 
<unit>...</unit> 
</clothes> 
<accessories> 
<unit> 
<title>Mug - Large</title> 
<color>Black</color> 
<price>6.99</price> 
<description> 
Large 16-ounce ceramic coffee mug, with witty use of IIT name on it. 
</description> 
</unit> 
<unit>...</unit> 
</accessories> 
</data> 

이 내가있어 코드입니다. 발생한 태그가 "books"인지 확인하고 있습니다.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict { 

    if([elementName isEqualToString:@"books"]) { 

     appDelegate.books = [[NSMutableArray alloc] init]; 

    } 

    else if([elementName isEqualToString:@"unit"]) { 

     aBook = [[Book alloc] init]; 
    } 

    NSLog(@"Processing Element: %@", elementName); 
} 

.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
     currentElementValue = [[NSMutableString alloc] initWithString:string]; 
    else 
     [currentElementValue appendString:string]; 

    NSLog(@"Processing Value: %@", currentElementValue); 

} 

여기에 나는 책 객체의 변수에 각 태그의 값을 할당하려합니다.

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

    if (qName) { 
     elementName = qName; 
    }  
    if (aBook) {    

    if ([elementName isEqualToString:@"title"]) { 
     aBook.title = currentElementValue; 
    } else if ([elementName isEqualToString:@"author"]) { 
     aBook.author = currentElementValue; 
    } else if ([elementName isEqualToString:@"isbn10"]) { 
     aBook.isbn10 = currentElementValue; 
    } else if ([elementName isEqualToString:@"isbn13"]) { 
     aBook.isbn13 = currentElementValue; 
    } else if ([elementName isEqualToString:@"pubDate"]) { 
     aBook.pubDate = currentElementValue; 

    } else if ([elementName isEqualToString:@"price"]) { 
     aBook.price = currentElementValue; 
    } else if ([elementName isEqualToString:@"description"]) { 
     aBook.description = currentElementValue; 
    } 

     [appDelegate.books addObject:aBook]; 

     [aBook release]; 
     aBook = nil;  

    } 

    [currentElementValue release]; 
    currentElementValue = nil; 
} 

나는

Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 
    [[cell textLabel] setText:[aBook title]]; 

문제는 모든 항목이 테이블보기뿐만 아니라 책에 표시됩니다이다하는 책의 목록을 표시하는 jQuery과가있다. 표시된 항목을 책에서만 볼 수있게 제한하려면 어떻게합니까? 파서 DidStartElement 메서드에서 잘못 될 것 같아요.

감사합니다.

답변

0

parser:didEndElement:namespaceURI:qualifiedName:에서 </books> 태그를 확인한 다음 그 이후에 어떤 <unit> 태그도 무시해야합니다. 당신이 </books> 태그 self.booksappDelegate.books를 설정 한 다음 전무로 self.books 설정에 도달 할 경우에만 그렇게 할 수

한 가지 방법은 다음 파서 내부 self.books 대신 appDelegate.books를 사용하는, 그리고 것입니다. <unit> 태그에 도달하면 self.books이 nil 인 경우 광고 단위를 무시합니다.

+0

이제 if ([elementName isEqualToString : @ "books"]) { \t \t // 배열을 초기화하십시오. \t \t self.books = [[NSMutableArray alloc] init]; \t \t \t }'didStartElement 메소드 및 '다른 경우 ([isEqualToString가 elementName @ "책"]) { \t \t [self.books addObject : aBook]; \t \t \t appDelegate.books = self.books; \t \t \t self.books = nil; doEndElement 메소드에서 ' \t \t'입니다. 언급 한 내용입니까? 나는이 & 내 tableView 비어 시도했다. – Bharat