How to parse the following XML?

﹥>﹥吖頭↗ 提交于 2019-12-24 08:35:47

问题


<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>

I am getting the above XML data from web service. I am not getting how to parse it.I would like to have 'colorNameArray' containing all the color names, 'colorNumberArray' containing all the color numbers and so on for all the details. Code for this with parser delegates is appreciable. Thank You


回答1:


This link gives a good understanding for xml parsing using nsxmlparser .(sample code available).

The values are present as the attributes so it can be obtained from attributeDict of didStartElement delegate method of nsxmlparser.

for ex:

- (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"]];

}   
}

All The Best.




回答2:


just use simple attribute XML Parsing below is sample code of that

-(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];

    }

}


来源:https://stackoverflow.com/questions/3912277/how-to-parse-the-following-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!