HTML Parsing Follow-up
HTML Parsing Follow-up
- Subject: HTML Parsing Follow-up
- From: Sam Goldman <email@hidden>
- Date: Mon, 21 Jan 2002 22:39:25 -0800
There was a short e-mail series on this list about HTML Parsing and it gave
me a little inspiration to get it working.
I came up with this:
- (void)awakeFromNib
{
NSString *testString = @"This <string> is <a> test <string>";
NSScanner *scanner = [[NSScanner alloc] initWithString:testString];
NSString *tmpString = [[[NSString alloc] init] autorelease];
NSMutableString *doneString = [[[NSMutableString alloc] init]
autorelease];
while ([scanner scanUpToString:@"<" intoString:&tmpString])
{
[doneString appendString:tmpString];
if ([scanner scanString:@"<" intoString:nil])
[doneString appendString:@"<"];
if ([scanner scanUpToString:@">" intoString:&tmpString])
{
[doneString appendString:tmpString];
if ([scanner scanString:@"> " intoString:nil])
[doneString appendString:@"> "];
else if ([scanner scanString:@">" intoString:nil])
[doneString appendString:@">"];
}
}
NSLog(doneString);
}
Now, for this example it works (Yay), but I am sure that many of you "gurus"
who are reading this are rolling your eyes and mumbling, "Yeah, until he
hits white space."
Example, try changing testString to @"This <is> a <test>
<string>"
This gives us (in the NSLog):
2002-01-21 22:38:08.268 HTML Parser[3924] This <is> a <test>
Uh, oh.
This is where I hit a wall, because the entire thing stops working. I know
that this is a problem with the way that NSScanner reacts (or doesn't react)
to spaces.
If anyone on this list could find it in their hearts to fix this up, Darin,
hopefully some other people, and I would be grateful.
Thanks y'all,
Sam
P.S. Also, this code isn't optimized since I don't know enough about that to
do so. Feel free to change ANYTHING.