<colors>
<color colorName="Abracadabra" colorNumber="D51-2" rColor="209" gColor="224" bColor="229" colorCollection="Harmony" colorFamily="Shaded" rating="3" surfaces="" colorGroup="Blues" />
<color colorName="Abyss" colorNumber="B50-1" rColor="233" gColor="247" bColor="249" colorCollection="" colorFamily="Clean" rating="3" surfaces="" colorGroup="Blues" />
<colors>
我从Web服务获取上述XML数据。我没有得到如何解析它。我希望'colorNameArray'包含所有颜色名称,'colorNumberArray'包含所有颜色数字等等所有细节。解析器委托的代码很明显。 谢谢
答案 0 :(得分:2)
This链接使用nsxmlparser很好地理解了xml解析。(示例代码可用)。
这些值作为属性存在,因此可以从nsxmlparser的didStartElement委托方法的attributeDict获取。
代表:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"color"])
{
NSString* colorName = [attributeDict objectForKey:@"colorName"]];
}
}
所有最好的。
答案 1 :(得分:0)
只需使用简单属性下面的XML解析是
的示例代码-(void)startParsingForFriendRequest:(NSString *)userID
{
NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/user_id=%@",userID];
////NSLog(@"urlString : %@",urlString);
NSURL *xmlURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];
NSURLResponse *returnedResponse = nil;
NSError *returnedError = nil;
NSData *itemData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
NSString *itemString = [[[NSString alloc] initWithBytes:[itemData bytes] length:[itemData length] encoding:NSUTF8StringEncoding]autorelease];
//NSLog(@"itemString : %@",itemString);
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:itemData];
[xmlParser setDelegate:self];
[xmlParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
////NSLog(@"parserDidStartDocument");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
////NSLog(@"parseErrorOccurred");
NSString * errorString = [NSString stringWithFormat:@"Error (Error code %i )", [parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading data" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
////NSLog(@"didStartElement");
//////NSLog(@"elementName : %@",elementName);
//////NSLog(@"namespaceURI : %@",namespaceURI);
//////NSLog(@"qualifiedName : %@",qualifiedName);
////NSLog(@"attributeDict : %@",attributeDict);
[registerNewArr addObject:attributeDict];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
////NSLog(@"foundCharacters");
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
////NSLog(@"didEndElement");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
if ([[[registerNewArr objectAtIndex:1]objectForKey:@"Transaction"]isEqualToString:@"loginxml"]) {
[(LoginViewController *)obj getRegisterResult:registerNewArr];
}
}