NSXMLParserTroubles (errors and line/column positions wrong?)
NSXMLParserTroubles (errors and line/column positions wrong?)
- Subject: NSXMLParserTroubles (errors and line/column positions wrong?)
- From: Jim Correia <email@hidden>
- Date: Fri, 23 Jan 2004 12:33:10 -0500
I've got a little sample program that uses NXSMLParser.
I'm actually feeding it broken XML to see how to handle error
conditions. Surprisingly, a few things happen.
1) lineNumber and columnNumber always report 0 (even in delegate
methods)
2) in XMLParserDelegate parser:parseErrorOccurred:] the parserError
always appears to be NSXMLParserInternalError.
3) After the parse completes, I get the expected parsing error:
NSXMLParserUnfinishedTagError
4) the documentation states that after I receive an error parsing is
stopped, but I get another error (so it would seem parsing hasn't
stopped) I can generate other input data which generates multiple
errors. (I haven't hooked up other delegate methods yet to see if they
are called after an error occurs - I wanted to have a sound
understanding of things before moving forward.)
Does anyone have experience using NSXMLParser? Are these just bugs? (In
which case I'll file a bug report.) Or am I using the class
incorrectly?
Thanks,
Jim
The XML that I am feeding it is:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
</foo>
And the code looks like:
#import <Foundation/Foundation.h>
@interface XMLParserDelegate : NSObject
{
}
@end
@implementation XMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary
*)attributeDict
{
NSLog(@"%s %@ %@ %@\nline: %d\ncolumn%d",
__PRETTY_FUNCTION__,
elementName,
namespaceURI,
qName,
[parser lineNumber],
[parser columnNumber]);
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError
*)parseError
{
NSLog(@"%s\n%@\n%@\nline: %d\ncolumn: %d",
__PRETTY_FUNCTION__,
parseError,
[parseError userInfo],
[parser lineNumber],
[parser columnNumber]);
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL fileURLWithPath:
@"/Users/correia/Desktop/xml-parser/test.xml"];
NSXMLParser *parser = [[[NSXMLParser alloc] initWithContentsOfURL:
url] autorelease];
XMLParserDelegate *delegate = [[[XMLParserDelegate alloc] init]
autorelease];
[parser setDelegate: delegate];
[parser parse];
NSLog(@"Done parsing");
NSLog(@"%@", [parser parserError]);
NSLog(@"%@", [[parser parserError] userInfo]);
NSLog(@"line: %d column: %d", [parser lineNumber], [parser
columnNumber]);
[pool release];
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.