iphone中的NSXML Parser

时间:2012-07-16 05:18:37

标签: iphone objective-c xml-parsing nsxmlparser

我需要获取以下xml的值,我使用NSXML解析器来解析这个

<boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>

我需要从上面的xml中获取“true”值。

我应该在NSXML解析器委托方法中做什么?

2 个答案:

答案 0 :(得分:0)

如果您想要真正的价值,请执行以下操作:

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        strVal=string;
    }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if([elementName isEqualToString:@"boolean"])
       {
          //you can get 'true' value here
       }

    }

答案 1 :(得分:0)

#import "XMLParser.h"

@implementation XMLParser

- (NSString *)parseXMLFile: (NSURL *) url
{   
    outstring = [[NSMutableString alloc] init];

        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL: url];

        [parser setDelegate: self];
    [parser parse];
        [parser release];

    return [outstring autorelease];
}

//<boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                              namespaceURI:(NSString *)namespaceURI 
                          qualifiedName:(NSString *)qName 
                          attributes:(NSDictionary *)attributeDict   
{
        if (qName) elementName = qName;
    if (elementName) current = [NSString stringWithString:elementName];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                         namespaceURI:(NSString *)namespaceURI 
                         qualifiedName:(NSString *)qName
{
    current = nil;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (!current) return;
    if ([current isEqualToString:@"boolean"]) 
        [outstring appendFormat:@"%@\n", string];
}

//  <boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>


-(void) parserDidStartDocument:(NSXMLParser *)parser {
    NSLog(@"parserDidStartDocument");   
}

-(void) parserDidEndDocument: (NSXMLParser *)parser {
    NSLog(@"parserDidEndDocument %@", outstring);   
}
@end

For more detail regarding the nsxml parse Reference site here.

这可能对你有所帮助。