XML to Xcode Parsing

I am parsing data from a XML file to my app in Xcode. it is parsing but first value is not showing in UITableView please help. let me show you my code.

ViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSLog(@"node content = %@",nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle=nodecontent;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink=nodecontent;
    }
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    }
    nodecontent=[[NSMutableString alloc]init];
}

And my XML file

<?xml version="1.0"?>
<data>
      <item><title>Eggs</title>
      <link>http://url/category.php?cat_id=15</link></item>
      <item><title>Bakery</title>
      <link>http://url/category.php?cat_id=15</link></item>
      <item><title>Bread</title>
      <link>http://url/category.php?cat_id=16</link></item>
      <item><title>Cakes, Pies Patisserie</title>
      <link>http://url/category.php?cat_id=17</link></item>
      <item><title>Specialty Breads</title>
      <link>http://url/category.php?cat_id=119</link></item>
</data>

now in output i can see all the entries but not first one's title "Eggs"

Answers


First, you shouldn't allocate your nodecontent in didEndElement method : you will have an allocated object when your parsing is over.

Try this way :

- (void)viewDidLoad {
    [super viewDidLoad];
    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    } else {
     nodecontent = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    } else if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle= nodecontent;
        nodecontent = nil;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink= nodecontent;
        nodecontent = nil;
    }
}

Yes, it is because you are initialise nodecontent string in NSXMLParser delegate method didEndElement:.

So what happen when parser found first object it will initialise nodecontent string and first object will not append to string because it is not initialise that time but for second object and all remains will be added in nodecontent as it is initialise after first call.

So better to alloc init nodecontent in viewDidLoad() method, before parsing XML file.

Remove line nodecontent=[[NSMutableString alloc]init]; from - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName method.

- (void)viewDidLoad {
    [super viewDidLoad];

    nodecontent=[[NSMutableString alloc]init];

    rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle=nodecontent;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink=nodecontent;
    }
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    }
}

Need Your Help

Can I change an Esper bolt statement on the fly?

storm esper topology

I'm using storm to process some data streams.

DRF Browsable API only shows one Router

django django-rest-framework django-urls python-3.4

Essentially, depending on the order in which I add my routes to my urlpatterns the browsable API will only show one router at a time. Here's my code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.