XML Parsing Done Wrong?

Joined
Oct 11, 2011
Messages
46
Reaction score
0
Points
6
Your Mac's Specs
13" MacBook Pro 2.4 GHz 8 GB Ram 250 GB HD, iPhone 4/S 64 GB, iPhone 3GS 16 GB, iPad 16 GB Wi-Fi
Im making a split view product search app for the iPad, that gets the data from a web service. but I'm having trouble getting the data to show up in my master tableview controller (the small table view on the left)


is my parsing done correctly?


Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
    
    NSString * newStr = [NSString stringWithUTF8String:[self.responseData bytes]];
    
    [self parseXML]; 
}



- (void) parseXML 
{
    NSLog (@"parseXML");
   
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:self.responseData];
    
    [xmlParser setDelegate:self];
    
    
    myMasterList = [[MasterList alloc] init];
    
    if (![xmlParser parse])
    {
        NSLog (@"An error occurred in the parsing");    
    }
}



- (void)parserDidStartDocument:(NSXMLParser *)parser 
{
    NSLog (@"parserDidStartDocument");
    inItemElement = NO;
}


- (void)parserDidEndDocument:(NSXMLParser *)parser 
{
    NSLog (@"parserDidEndDocument");
}


// Called when the parser encounters a start element
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
{
    if ([elementName isEqualToString:@"str_smartsearch"]) 
    {
        inItemElement = YES;
    }
    capturedCharacters = [[NSMutableString alloc] initWithCapacity:50];
    capturedCharacters = [[NSMutableString alloc] initWithCapacity:250];
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
    
    
    if ([elementName isEqualToString:@"description"])
    {
        myMasterList.masterDecript = capturedCharacters;
        NSLog(@" decription:%@", capturedCharacters);
        }
    if ([elementName isEqualToString:@"onhand"]) 
    {
        myMasterList.masterOnHand = capturedCharacters;
        
    }
    if ([elementName isEqualToString:@"price"])
    {
        myMasterList.masterPrice = capturedCharacters;
    }
    if ([elementName isEqualToString:@"itemno"])
    {
        myMasterList.masterItem = capturedCharacters;
        [masterArray addObject:myMasterList];
        myMasterList = nil;
   
        
    }
    
    capturedCharacters = nil;   
    
    if ([elementName isEqualToString:@"str_smartsearch"]) 
    {
     
       inItemElement = NO;
    }
    

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string 
{
    if (capturedCharacters != nil) 
    {
        [capturedCharacters appendString:string];
   
    }
    else 
    {
        [capturedCharacters isEqual:string];
    }
    
}


I've token out the NS logs and such, to make it easier to read. i've been stuck on this for while.


all i need is for it to show up on my master tableview and iill basically be done with this app.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Your code is a little confusing as your using iVars that we cant see in the posted code.
Also you have'nt implemented all of the NSURLConnection delegate methods, or all of the
NSXMLParser delegate methods either.

Code:
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

This method is very important, to catch errors if the connection fails, or is interrupted.

But I also noticed that you are not calling the parse method of the NSXMLParser, like this.

Code:
NSMutableData *xmlData;

NSXMLParser *parser = [[NSXMLParser alloc] initWithData: xmlData];

[parser setDelegate: self];

[parser parse];    //You have to make this method call to start the data parsing

[parser release];    //You can release the parser straight away as it blocks until finished

Also make sure you implement all of the NSXMLParser delegate XML methods, even if you dont want to use them, and simply put NSLog statements in each method to check the data flow.

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40008632

Also do the same thing with the NSURLConnection delegate methods.

https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40009947

Good luck with it.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top