-1

私はこの単純な XML を持っており、それを解析し、その子データを別のビュー コントローラーに渡しています。私の XML は次のようになります。

<categories>
  <category>
    <name>Electronics</name>
    <description>This is the given sample description of main menu</description>
    <image>Link Here</image>
    <sub_cat>
       <sub_name>Laptop</sub_name>
       <sub_desc>sub cat description of Laptop</sub_desc>
       <sub_image>Link Here</sub_image>
    </sub_cat>
    <sub_cat>
       <sub_name>Printers</sub_name>
       <sub_desc>sub cat description of Printers</sub_desc>
       <sub_image>Link Here</sub_image>
    </sub_cat>
  </category>
  <category>
    <name>Food</name>
    <description>This is the given sample description of main menu</description>
    <image>Link Here</image>
    <sub_cat>
        <sub_name>Pizza</sub_name>
        <sub_desc>sub cat description of pizza</sub_desc>
        <sub_image>Link Here</sub_image>
    </sub_cat>
    <sub_cat>
        <sub_name>Burgers</sub_name>
        <sub_desc>sub cat description of Burgers</sub_desc>
        <sub_image>Link Here</sub_image>
    </sub_cat>
  </category>
  <category>
     <name>Gifts</name>
     <description>This is the given sample description of main menu</description>
     <image>Link Here</image>
     <sub_cat>
          <sub_name>Photo Albums</sub_name>
          <sub_desc>sub cat description of Photo Album</sub_desc>
          <sub_image>Link Here</sub_image>
     </sub_cat>
     <sub_cat>
          <sub_name>Car</sub_name>
          <sub_desc>sub cat description of Car</sub_desc>
          <sub_image>Link Here</sub_image>
     </sub_cat>
   </category>
</categories>

そして、単純な XML として NSXMLParser を使用してこの配列を解析し、次のように解析します。

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


    if([elementName isEqualToString:@"category"]){

        _mainCategory = [[NSMutableDictionary alloc]init];
    } else if([elementName isEqualToString:@"sub_cat"]){
        _subCategory = [[NSMutableDictionary alloc]init];
    }

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

    currentData  = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

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

    if ([elementName isEqualToString:@"name"]) {
        [_mainCategory setValue:currentData forKey:elementName];
    }

    if ([elementName isEqualToString:@"description"]){
        [_mainCategory setValue:currentData forKey:elementName];
    }
    if ([elementName isEqualToString:@"image"]) {
        [_mainCategory setValue:currentData forKey:elementName];
    }

    if ([elementName isEqualToString:@"sub_name"]) {
        [_subCategory setValue:currentData forKey:elementName];
    }
    if ([elementName isEqualToString:@"sub_desc"]) {
        [_subCategory setValue:currentData forKey:elementName];

    }
    if ([elementName isEqualToString:@"sub_cat"]) {
        [_childPopulated addObject:_subCategory];

    }

    if([elementName isEqualToString:@"category"]){


        [_mainCategory setValue:_childPopulated forKey:@"sub_cat"];

        [_listPopulated addObject:_mainCategory];

        _mainCategory = nil;
        _childPopulated = nil;

    }


}

出力データは次のようになります。

{
        description = "This is the given sample description of main menu";
        image = "Link Here";
        name = Electronics;
        "sub_cat" =    (
                        {
                "sub_desc" = "sub cat description of Laptop";
                "sub_name" = Laptop;
            },
                        {
                "sub_desc" = "sub cat description of Printers";
                "sub_name" = Printers;
            }
        );
    },
        {
        description = "This is the given sample description of main menu";
        image = "Link Here";
        name = Food;
    },
        {
        description = "This is the given sample description of main menu";
        image = "Link Here";
        name = Gifts;
    }

しかし、私の予想されるデータは、sub_cat をキーとする配列の最初のインデックスのようになるはずです。しかし、2 番目のサブカテゴリに入ると、他の sub_cat を取得しません。

私の期待値は次のようになります。

 {
     description = "This is the given sample description of main menu";
     image = "Link Here";
     name = Electronics;
     "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Laptop;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Printers;
                }
            );
     description = "This is the given sample description of main menu";
     image = "Link Here";
     name = Food;
     "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Pizza;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Burger;
                }
            );
      description = "This is the given sample description of main menu";
      image = "Link Here";
      name = Gift;
      "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Car;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Photo Album;
                }
            );
 }

これを解析する際に何が欠けているのかわかりません。サブカテゴリの残りの部分が表示されない理由を知りたかっただけです。

4

2 に答える 2

1

あなたの期待値はこのようになるはずです

{
    {
     description = "This is the given sample description of main menu";
     image = "Link Here";
     name = Electronics;
     "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Laptop;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Printers;
                }
            );
},
   {  description = "This is the given sample description of main menu";
     image = "Link Here";
     name = Food;
     "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Pizza;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Burger;
                }
            );
},
     { description = "This is the given sample description of main menu";
      image = "Link Here";
      name = Gift;
      "sub_cat" =    (
                            {
                    "sub_desc" = "sub cat description of Laptop";
                    "sub_name" = Car;
                },
                            {
                    "sub_desc" = "sub cat description of Printers";
                    "sub_name" = Photo Album;
                }
            );
}
 }
于 2013-09-03T10:21:45.233 に答える
1

2つのこと:

おそらく、-setObject:forKey:オブジェクトを辞書に入れるために使用する必要があります。よりわずかに高速で-setValue:forKey:あり、キーを特別な方法で処理しません。

また、可変配列を に割り当てることは決してないようです_childPopulated。これが問題の原因である可能性があります。

于 2013-09-03T10:42:32.727 に答える