2011-08-02 14 views
-1
?xml version="1.0" encoding="UTF-8" ?> <IPAD version="1.0"> 
<articleSnapshot> 
<item> 
<categoryName>Kerala</categoryName> 
<oid>2915</oid> 
<title>ÎÆcÈÏ¢: ÄßøáJW ÕçKAá¢</title> 
<thumbImage>http://122.248.234.55/v3/repository/images/news/bar-liquor_thuthumb_2915.jpg</thumbImage> 
<shortDescription>ÎÆcÈÏÕᢠ¥Äßæa dÉ~cÞÉÈÕáÎÞÏß ÌtæMGí ©ÏVK ÕßÎVÖÈBZ ÏáÁß®Ëá¢...</shortDescription> 
</item> 
</articleSnapshot> 
</IPAD> 

このXMLファイルを解析して、viewdidload.iに表示したいのですが、画像ビューとテキストビューではcoding.but cant displayが完了しました。私に手伝ってください。ipadのxmlファイルを解析する

答えて

0

これは、あなたのものに非常によく似たXMLファイルを解析する方法です。これは基本的にXMLをplistファイルに変換します。

#pragma mark - Delegate Received 

-(void)parsingComplete:(XMLDataSource*)theParser { 
    NSArray *categoryArray = [theParser getCategories]; 
    [categoryArray writeToFile:PLIST atomically:YES]; 
} 
- (void)receivedItems:(NSArray *)theItems { 
    NSMutableArray *dataItems = [[NSMutableArray alloc] init]; 
    [dataItems addObjectsFromArray:[NSArray arrayWithContentsOfFile:PLIST]]; 

    NSDictionary * dataItem = [dataItems objectAtIndex:categoryIndex]; 
    data_ = [dataItem objectForKey:@"images"]; 
} 

#pragma mark - Parsing Delegates 

- (void)parse:(NSData *)data withDelegate:(id)sender onComplete:(SEL)callback 
{ 
    parentDelegate = sender; 
    onCompleteCallback = callback; 
    loading = YES; 

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 
    [parser setDelegate:self]; 
    [parser setShouldProcessNamespaces:NO]; 
    [parser setShouldReportNamespacePrefixes:NO]; 
    [parser setShouldResolveExternalEntities:NO]; 
    [parser parse]; 
} 
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    parsed = NO; 
    loading = NO; 
} 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    currentElement = element; 
    if ([[currentElement lowercaseString] isEqual:@"image"]) { 
     inImage = YES; 
     itemsDictionary = [[NSMutableDictionary alloc] init]; 
     [itemsDictionary addEntriesFromDictionary:attributeDict]; 
    } 

    if ([[currentElement lowercaseString] isEqual:@"category"]) { 
     inCategory = YES; 
     categoryDictionary = [[NSMutableDictionary alloc] init]; 
     [categoryDictionary addEntriesFromDictionary:attributeDict]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
{ 
    NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    if ([[element lowercaseString] isEqual:@"image"]) { 
     inImage = NO; 
     [items addObject:[itemsDictionary copy]]; 
    } 

    if ([[element lowercaseString] isEqual:@"category"]) { 
     inCategory = NO; 
     [categoryDictionary setObject:[items copy] forKey:@"images"]; 
     [items removeAllObjects]; 
     [categories addObject:[categoryDictionary copy]]; 
    } 
} 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    NSString *stringValue = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *element = [currentElement stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    /* skip over blank elements. */ 
    if (stringValue == nil || [stringValue isEqual:@""]) { 
     return; 
    } 

    if (element != nil && [element length] > 0) { 
     if (inImage) { 
      if ([itemsDictionary objectForKey:element] != nil) { 
       [itemsDictionary setObject:[NSString stringWithFormat:@"%@%@", [itemsDictionary objectForKey:element], stringValue] 
            forKey:element]; 
      } else { 
       [itemsDictionary setObject:stringValue forKey:element]; 
      } 
     } else if ((!inImage) && (inCategory)) { 
      if ([categoryDictionary objectForKey:element] != nil) { 
       [categoryDictionary setObject:[NSString stringWithFormat:@"%@%@", [categoryDictionary objectForKey:element], stringValue] 
             forKey:element]; 
      } else { 
       [categoryDictionary setObject:stringValue forKey:element]; 
      } 
     } else { 
      if ([root objectForKey:element] != nil) { 
       [root setObject:stringValue forKey:element]; 
      } 
     } 
    } 
} 
- (void)parserDidStartDocument:(NSXMLParser *)parser { 
    NSError * err; 
    if(![[NSFileManager defaultManager] fileExistsAtPath:PLIST]) { 
     [[NSFileManager defaultManager] removeItemAtPath:PLIST error:&err]; 
    } 
} 
- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 
    parsed = YES; 
    loading = NO; 

    if ([parentDelegate respondsToSelector:onCompleteCallback]) { 
     [parentDelegate performSelector:onCompleteCallback withObject:self]; 
    } 

    if ([self respondsToSelector:@selector(receivedItems:)]) 
     [self receivedItems:items]; 
    else 
     SHOW_ALERT(@"parserDidEndDocument:", @"receivedItems: not responding!", nil, @"Okay", nil) 
} 


#pragma mark - 
#pragma mark Setters/Getters 

- (id)delegate { 
    return self; 
} 
- (void)setDelegate:(id)new_delegate { 
    _delegate = self; 
} 
- (BOOL)isSuccessful { 
    return success; 
} 
- (BOOL)isLoading { 
    return loading; 
} 
- (BOOL)isParsed { 
    return parsed; 
} 
- (NSArray *)getCategories { 
    return categories; 
} 
- (NSDictionary *)getRoot { 
    return root; 
} 
- (void)dealloc { 
    [super dealloc]; 
} 
+0

しかし、私の文字列catogery、thumbimage、title、oid、shortdiscriptionおよびmutable array articleスナップショットを配置する場所。 –

+0

これはXMLからキーを定義する必要はなく、キー/値を取得します。 – WrightsCS

+0

私はそれを理解していませんでした、私は上記のXMLファイルで何ができるのか解読してください。 –

関連する問題