2011-05-06 83 views
0

我不知道如何使用NSXMLParser。我有一個服務器這個XML,我需要用戶ID消息。NSXMLParser問題

<?xml version="1.0" encoding="UTF-8"?><response value="1"> 
    <user id="100"> 
    <field name="name">DATO</field> 
    <field name="lastname">DATO</field> 
    <field name="country">DATO</field> 
    <field name="usstate">DATO</field> 
    <field name="email">DATO</field> 

    </user> 
    <message>Login successfully</message> 
    </response> 

我能走到今天

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:myResult]; 
[parser setDelegate:self]; // The parser calls methods in this class 
[parser setShouldProcessNamespaces:NO]; // We don't care about namespaces 
[parser setShouldReportNamespacePrefixes:NO]; // 
[parser setShouldResolveExternalEntities:NO]; // We just want data, no other stuff 

[parser parse]; // Parse that data.. 
[parser release]; 

而且實現了這個梅託德:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
//NSLog(@"user"); 
if ([elementName isEqualToString:@"user"]) 
{ 
    NSLog(@"user"); 
    //NSString(@""); 
} 
} 

我知道這很容易,但我找不到answer.Thanks。

回答

0

id的值可以在傳遞給解析器的attributeDict中找到:didStartElement:namespaceURI:qualifiedName:attributes:method。要訪問id屬性的值,這應該工作:

[attributeDict objectForKey:@"id"]