开发者

Parsing xml using NSXMLParser

I am trying to parse an xml file using NSXMLParser. My NSXMLParserDelegate implements parser:didStartElement and parser:didEndElement and it is getting messages from the parser for each element. But, when I try to print them I find that the messages are empty. What am I missing?

My delegate:

@interface ExtensionXML : NSObject <NSXMLParserDelegate> {
    @private NSXMLParser* xmlParser;
    @private NSURL*       fileUrl;
}
- (void) parse;           
- (ExtensionXML*) initWithPath: (NSString*)path;
@end

@implementation ExtensionXML
- 开发者_运维技巧(ExtensionXML*) initWithPath: (NSString*)path
{
     if (self = [super init])
     {
         fileUrl = [NSURL fileURLWithPath:[path  stringByExpandingTildeInPath]]; // error handling
     }
    return self;
}

- (void) parse
{
   xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:fileUrl];
   [xmlParser setDelegate:self];
   [xmlParser setShouldResolveExternalEntities:YES];
   [xmlParser setShouldProcessNamespaces:YES];
   [xmlParser setShouldReportNamespacePrefixes:YES];
   [xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"start: element %s uri %s qualified name %s", elementName, namespaceURI, qualifiedName);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"end: element %s uri %s qualified name %s", elementName, namespaceURI, qualifiedName);
}
@end 

The xml file is:

<someroot xmlns="http://ns.mycompany.com/product/format/1.3">
    <id>myid</id>
    <version>1</version>
    <components>
        <component name="componentOne">
                <initializer>Init0</initializer>
        </component>
    </components>
</someroot>

The output from NSLog shows that elementName, namespaceURI and qualifiesName are all empty.


To log NSStrings, use %@ instead of %s. Change both NSLog lines. See String Format Specifiers for a list of specifiers with their usage.

Another problem is the didEndElement delegate method does not have an attributes parameter so the method you've written won't get called. It should be:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI 
    qualifiedName:(NSString *)qualifiedName
{
    NSLog(@"end: element %@ uri %@ qualified name %@", 
                elementName, namespaceURI, qualifiedName);
}


Try to compare with element name and get the results like the following

if([elementName isEqualToString:@"component"])
{
 //print something related to xml data
}

Use %@ instead of %s for printing.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜