I have an xml document:
I have set up a cocoa application for testing purposes. The application consists of a single file. Inside that, this is the code. Header and Implementation file appears together below.
The problem is that when I press the "do" button (the one that activates the "do" IBAction) I get the following result:
The application appears to give more new lines than those exists inside the file. Why all those '\n' characters?
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE addresses SYSTEM "addresses.dtd">
<addresses>
<person>
<lastName>Doe</lastName>
<firstName>John</firstName>
<email>jdoe@foo.com</email>
<address>
<street>100 Main Street</street>
<city>Somewhere</city>
<state>New Jersey</state>
<zip>07670</zip>
</address>
</person>
</addresses>
I have set up a cocoa application for testing purposes. The application consists of a single file. Inside that, this is the code. Header and Implementation file appears together below.
Code:
#import <Cocoa/Cocoa.h>
#import "ABPerson.h"
@interface SFXMLParser : NSObject {
NSXMLParser *addressParser;
NSMutableArray *addresses;
ABPerson *currentPerson;
NSMutableString *currentStringValue;
NSMutableArray *dictProperties;
NSString *currentName;
}
- (void)parseXMLFile:(NSString *)pathToFile;
-(IBAction)do:(id)sender;
@end
#import "SFXMLParser.h"
@implementation SFXMLParser
- (id) init
{
self = [super init];
if (self != nil) {
}
return self;
}
-(void)awakeFromNib
{
NSString *str = [[NSString stringWithString:@"~/Desktop/info.xml"]stringByStandardizingPath];
[self parseXMLFile:str];
}
- (void)parseXMLFile:(NSString *)pathToFile {
BOOL success;
NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
if (addressParser) // addressParser is an NSXMLParser instance variable
[addressParser release];
addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[addressParser setDelegate:self];
[addressParser setShouldResolveExternalEntities:YES];
success = [addressParser parse]; // return value not used
// if not successful, delegate is informed of error
NSLog(@"parse!");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ( [elementName isEqualToString:@"addresses"]) {
// addresses is an NSMutableArray instance variable
NSLog(@"found addresses");
if (!addresses)
addresses = [[NSMutableArray alloc] init];
return;
}
if ( [elementName isEqualToString:@"person"] ) {
NSLog(@"found ABPerson");
// currentPerson is an ABPerson instance variable
currentPerson = [[ABPerson alloc] init];
return;
}
if ( [elementName isEqualToString:@"lastName"] ) {
NSLog(@"found lastName");
// currentPerson is an ABPerson instance variable
currentPerson = [[ABPerson alloc] init];
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSLog(@"found characters:%@",string);
if (!currentStringValue) {
// currentStringValue is an NSMutableString instance variable
currentStringValue = [[NSMutableString alloc] init];
}
NSLog(@"appendingstring...");
[currentStringValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (( [elementName isEqualToString:@"addresses"]) ||
( [elementName isEqualToString:@"address"] )) return;
if ([elementName isEqualToString:@"lastName"]) {
NSLog(@"lastname entering...%@",currentStringValue);
[currentPerson setLastName:currentStringValue];
}
if ([elementName isEqualToString:@"firstName"]) {
NSLog(@"first name entering...");
[currentPerson setFirstName:currentStringValue];
}
if ([elementName isEqualToString:@"email"]) {
NSLog(@"email entering...");
[currentPerson setEmail:currentStringValue];
}
if ( [elementName isEqualToString:@"person"] ) {
[addresses addObject:currentPerson];
[currentPerson release];
return;
}
[currentStringValue release];
currentStringValue = nil;
}
-(IBAction)do:(id)sender
{
int i = 0;
NSLog(@"first name:%@\nlast name:%@\nemail:%@",[[addresses objectAtIndex:i]firstName], [[addresses objectAtIndex:i]lastName], [[addresses objectAtIndex:i]email]);
}
- (void) dealloc
{
[dictProperties release];
[super dealloc];
}
@end
The problem is that when I press the "do" button (the one that activates the "do" IBAction) I get the following result:
Code:
2008-02-06 18:13:04.906 cocoaki2[582:10b] first name:
John
last name:
Doe
email:
jdoe@foo.com
The application appears to give more new lines than those exists inside the file. Why all those '\n' characters?