【问题标题】:parse xml and store each element in array in iphone解析xml并将每个元素存储在iphone的数组中
【发布时间】:2012-09-04 09:24:56
【问题描述】:

我想将每个元素存储在数组中。喜欢这个链接

在此链接中有多个标签,例如 Slider、Latest Headlines、Slider1 等

有滑块和最新的标题,所以在这个相同的键缩略图和 contentId 中我想存储在单独的数组中。

谁能告诉我如何解析它

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。

请给我建议。

谢谢

【问题讨论】:

标签: iphone xml arrays


【解决方案1】:

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

或查看本教程tutorial for NSXML

【讨论】:

  • 先告诉我你用的是TBXML还是NSXMl
  • 使用 TBXML 很简单,...制作 nsmutabledictionary 并将数据保存到字典,然后将此字典保存到 nsmutablearray
【解决方案2】:

//使用 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.
   }

}

这将为您提供一个以 contentid 作为键和缩略图作为值的字典。希望这会给您一个公平的想法,以便从 xml 中获取您想要的任何内容

【讨论】:

  • @user1011291 好的,我会在这里根据你的 xml 编辑我的答案。
  • 您好,感谢您的回复,但您可以看到有两个标签 Slider 和 latestheadlines 好的,我想根据标签获取缩略图。
  • 我很欣赏 naveen,但我必须根据 Slider 和 LatestHeadlines 在 tableview 中显示数据。如果有滑块,则滑块标签的数据将显示为与最新标题相同。请帮帮我
  • 嗨,naveen,你有没有找到我正在为这个链接做的事情,请告诉我bizfy.com/demo/biscoot/response1.php
猜你喜欢
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2015-02-06
相关资源
最近更新 更多