-1

This is the code of my app . The applications runs but there is no data displayed , can you help me to fix this problem ?

In .h file:

@interface MyData : NSObject <NSXMLParserDelegate> {

        NSMutableString *currentElementValue;
        User *user;
        NSMutableArray *users;
    }

    -(MyData *) initXMLParser;
    -(BOOL)parseDocumentWithData:(NSData *)data; 

    @property (nonatomic, retain) User *user;
    @property (nonatomic, retain) NSMutableArray *users;

In .m file:

@implementation MyData
@synthesize user;
@synthesize users;


- (MyData *) initXMLParser {
    [super init];
    // init array of user objects 
    users = [[NSMutableArray alloc] init];
    return self;
}

-(BOOL)parseDocumentWithData:(NSData *)data {
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Users" ofType:@"xml"];
    data = [NSData dataWithContentsOfFile:filePath];

    if (data == nil)
        return NO;

    // this is the parsing machine
    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];

    // this class will handle the events
    [xmlparser setDelegate:self];
    [xmlparser setShouldResolveExternalEntities:NO];

    // now parse the document
    BOOL ok = [xmlparser parse];
    if (ok == NO)
        NSLog(@"error");
    else
        NSLog(@"OK");

    [xmlparser release];
    return ok;
}

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"user"]) {
        NSLog(@"user element found – create a new instance of User class...");
        user = [[User alloc] init];

    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (!currentElementValue) {
        // init the ad hoc string with the value     
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    } else {
        // append value to the ad hoc string    
        [currentElementValue appendString:string];
    }
    NSLog(@"Processing value for : %@", string);
}  

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"users"]) {
        // We reached the end of the XML document
        return;
    }

    if ([elementName isEqualToString:@"user"]) {

        // object to our user array
        [users addObject:user];
        // release user object
        [user release];
        user = nil;
    } else {

        [user setValue:currentElementValue forKey:elementName];
    }

    [currentElementValue release];
    currentElementValue = nil;
}

@end

/* This is the code of my app . The applications runs but there is no data displayed , can you help me to fix this problem ? */

This is the code of my app . The application runs but there is no data displayed , can you help me to fix this problem ? I want to display data when parsing

Monolo
  • 18,205
  • 17
  • 69
  • 103
user1503496
  • 191
  • 4
  • 13

1 Answers1

0

Well if you are using the NSXML parser (objective-c) you will need to append the data from the parsing of the xml element, to a string. How are you planning to display the data from the xml file.

You can apply the same method from here:

Techno Buffalo RSS

And then change the values in the parser to your element values.

Philip Laine
  • 458
  • 3
  • 17