-2

我需要获取以下 xml 的值,我正在使用 NSXML 解析器来解析它

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

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

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

4

2 回答 2

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
       }

    }
于 2012-07-16T05:26:44.797 回答
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

有关 nsxml 解析参考站点的更多详细信息,请单击此处。

这可能对你更有帮助。

于 2012-07-16T05:31:39.823 回答