2012-09-04 44 views
0

我想將每個元素存儲在數組中。像這樣的鏈接解析xml並將每個元素存儲在iphone中的數組中

在這個環節有喜歡滑蓋多個每日新聞,最新的頭條新聞,Slider1等

有滑塊和最新的頭條新聞所以在這個相同的密鑰縮略圖,內容識別我想在不同的陣列來存儲。

任何一個讓我知道如何分析它

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> 

我要分析此類型的XML。

請給我建議。

感謝

+0

按照這些幫助... 1. http://stackoverflow.com/questions/10877374/how-to-parse-通過xml解析器/ 10877758#10877758這個屬性就像這個和這一個2. http://stackoverflow.com/questions/10064262/reading-xml-file-in-iphone –

回答

0

//使用的NSXMLParser

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

//下面是這將讓你的數據

-(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. 
    } 

} 

這將讓你與內容識別關鍵一百科的代表和thumbnail as value.Hope這會給你一個公平的想法,從xml中獲得你想要的任何東西

+0

嗨,我編輯了我的問題請看這個。 – user1011291

+0

@ user1011291好的,我會根據你的xml在這裏編輯我的答案。 – superGokuN

+0

嗨感謝您的回覆,但你可以看到有兩個標籤滑塊和最新頭條好的我想根據標籤得到縮略圖。 – user1011291

相關問題