NSXMLParser问题

时间:2011-05-06 20:25:20

标签: xml ios parsing

我不知道如何使用NSXMLParser。我从服务器获取此xml,我需要用户ID消息。

<?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(@"");
}
}

我知道这很容易,但我找不到答案。谢谢。

1 个答案:

答案 0 :(得分:0)

id的值可以在传递给解析器的attributeDict中找到:didStartElement:namespaceURI:qualifiedName:attributes:method。要访问id属性的值,这应该有效:

[attributeDict objectForKey:@"id"]