解析xml并将每个元素存储在iphone中的数组中

问题描述:

我想将每个元素存储在数组中。像这样的链接解析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://*.com/questions/10877374/how-to-parse-通过xml解析器/ 10877758#10877758这个属性就像这个和这一个2. http://*.com/questions/10064262/reading-xml-file-in-iphone –

//使用的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

使用TBXML解析XML内容..
http://tbxml.co.uk/

或检查本教程​​

+0

嗨,我有编辑我的问题请看这 – user1011291

+0

首先告诉我哪一个你使用TBXML或NSXMl – Rajneesh071

+0

嗨,我使用NSXML – user1011291