0

I want to store each element in array. Like this link

In this link there are multiple tages like Slider, Latest Headlines, Slider1 etc

There are slider and latest Headlines so in this same key thumbnail and contentId i want to store in separate array.

any one let me know how to parse it

http://bizfy.com/demo/biscoot/response1.php

 <contentResponse>
  <slider>
   <item>
   <thumbNail>http://www.bizfy.com/demo/biscoot/a1.png</thumbNail>
   <contentId>img001</contentId>
 </item>
<item>
<thumbNail>http://www.bizfy.com/demo/biscoot/a1.png</thumbNail>
<contentId>img002</contentId>
 </item>
 <item>
 <thumbNail>http://www.bizfy.com/demo/biscoot/a1.png</thumbNail>
     <contentId>img003</contentId>
    </item>
   <item>
    <thumbNail>http://www.bizfy.com/demo/biscoot/a1.png</thumbNail>
       <contentId>img004</contentId>
       </item>
       <item>
   <thumbNail>http://www.bizfy.com/demo/biscoot/a1.png</thumbNail>
     <contentId>img005</contentId>
       </item>
        </slider>
       <latestHeadlines>
        <item>
       <thumbNail>http://www.bizfy.com/demo/biscoot/a2.png</thumbNail>
       <heading>SRKs Kashmir NOstalgia</heading>
          <shortDescription>
      Being in Kashmir has made Shah Rukh Khan nostalgic as the superstar's father always              wanted him to visit the valley. "My father's one unfulfilled wish was to bring me to Kashmir          because his mom was from here.
          </shortDescription>
          <views>369</views>
           <rating>3</rating>
          <contentId>news001</contentId>
              </item>
              <item>
             <thumbNail>http://www.bizfy.com/demo/biscoot/a2.png</thumbNail>
           <heading>SRKs Kashmir NOstalgia</heading>
             <shortDescription>
               Being in Kashmir has made Shah Rukh Khan nostalgic as the superstar's      father always wanted him to visit the valley. "My father's one unfulfilled wish was to bring me to Kashmir because his mom was from here.
             </shortDescription>
              <views>369</views>
                 <rating>3</rating>
              <contentId>news001</contentId>
            </item>
              <item>
           <thumbNail>http://www.bizfy.com/demo/biscoot/a2.png</thumbNail>
            <heading>SRKs Kashmir NOstalgia</heading>
          <shortDescription>
                 Being in Kashmir has made Shah Rukh Khan nostalgic as the superstar's father always wanted him to visit the valley. "My father's one unfulfilled wish was to bring me to Kashmir because his mom was from here.
           </shortDescription>
               <views>369</views>
               <rating>3</rating>
               <contentId>news001</contentId>
                     </item>
                </latestHeadlines>
               </contentResponse>

I want to parse this type of xml.

Please suggest me.

Thanks

user1011291
  • 109
  • 1
  • 2
  • 12
  • Follow these helps... 1. http://stackoverflow.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758 and this one 2. http://stackoverflow.com/questions/10064262/reading-xml-file-in-iphone – Khalid Usman Sep 04 '12 at 10:13

2 Answers2

0

//USE NSXMLParser

  NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"YOUR_URL"]];
  [parser setDelegate:self];
  [parser parse];

// Below are the delegates which will get you the data

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

  // This delegate is called whenever parser hits the open tag in the xml here i am checking that if current tag is what we are looking for or not if yes then set a bool to gets it value in next delegate
  if([elementName isEqualToString:@"slider"]){
       getslider = YES; // getslider is a bool which is NO initialy
   }

  if([elementName isEqualToString:@"latestHeadline"]){
       getlatest = YES; // getlatest is a bool which is NO initialy
   }

}


  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

      //This delegate gives the data for the tag encountered.
        if(getslider)
      {
        [sliderArray addObject:string];//sliderArray a mutablearray which will contains the value of every single tag nested inside slider tag
      }

        if(getlatest)
      {
        [latestArray addObject:string]; //same here this will have every thing inside latestHeadline tag.
      }

 }

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

    // This delegate is called whenever parser hit the end tag.

    if([elementName isEqualToString:@"slider"]){
       getslider = NO;// we encountered the slider end tag.
   }


    if([elementName isEqualToString:@"latestHeadlines"]){
       getlatest = NO; // we encountered the latestHeadlines end tag.
   }

}

This will get you a dictionay with contentid as key and thumbnail as value.Hope this will give you a fair idea to get anything you want from the xml

superGokuN
  • 1,399
  • 13
  • 28
  • @user1011291 ok i'll edit my answer according to your xml here. – superGokuN Sep 04 '12 at 10:08
  • Hi Thanks for reply but you can see there are two tags Slider and latestheadlines ok i want to get thumbnail according to tags. – user1011291 Sep 04 '12 at 10:15
  • i appreciate naveen but i have to show data in tableview according to Slider and LatestHeadlines. If there is slider then Data will show for slider tages same as for latestheadlines. Please help me – user1011291 Sep 04 '12 at 10:27
  • Hi naveen have you find something I am doing for this link please tell me http://bizfy.com/demo/biscoot/response1.php – user1011291 Sep 05 '12 at 06:26
0

Use TBXML to parse XML content..
http://tbxml.co.uk/

or check this tutorial tutorial for NSXML

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74