0

各要素を配列に格納したい。このリンクのように

このリンクには、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を解析したい。

私に提案してください。

ありがとう

4

2 に答える 2

0

//USE 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.
   }

}

これにより、コンテンツ ID をキーとして、サムネイルを値として持つ辞書が取得されます。これにより、xml から必要なものを取得するための公正なアイデアが得られることを願っています。

于 2012-09-04T09:29:38.507 に答える
0

TBXML を使用して XML コンテンツを解析します。
http://tbxml.co.uk/

または、NSXML のこのチュートリアル チュートリアルを確認してください

于 2012-09-04T09:30:01.503 に答える