当xml解析中找不到子元素时,如何向数组添加默认值?

发布于 2024-08-20 23:19:40 字数 2072 浏览 3 评论 0原文

我是 iphone 开发的新手。我正在解析 XML 页面并在 tableview 中显示内容。在 XML 页面中的某个块中,缺少子元素,我想在数组中添加一个值 o,当子元素不喜欢特定的块。我的 XML 文件就像

 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> ..................................>first block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 </entry> ....................................>Second block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> .....................................>Third block

我正在解析 gd: ratings 标签并在表视图中显示其属性 numRates 值。我有一个可变数组,用于存储 numRates 的值。解析第二个块时,我必须在数组中添加对象“O”,因为它不包含 gd: ratings 标记。由于该值存在于属性标记中,对于检索内容并将其添加到可变数组是在 NSXML 解析器 DidStartElement 方法中完成的。

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

currentElement = [elementName copy];
if ([elementName isEqualToString:@"entry"]) {

    entry = [[NSMutableDictionary alloc] init];
    currentTitle = [[NSMutableString alloc] init];
    currentDate = [[NSMutableString alloc] init];
    NSLog(@"inside image1 ");
}else if([elementName isEqualToString:@"media:thumbnail"])
    { currentImage = [[NSMutableString alloc] init];
        if(myUrl == nil){   
        myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
        }
        [currentImage appendString:myUrl];  
        [stories1 addObject:currentImage];
        myUrl=nil;
        }   else if([elementName isEqualToString:@"gd:rating"])
{
    currentRating=[[NSMutableString alloc]init];

        myRatings=[NSString stringWithString:[attributeDict objectForKey:@"numRaters"]];

    [currentRating appendString:myRatings];
    [stories2 addObject:currentRating];
}   
}

由于有 25 个条目标签块,并且其中 10 个块没有 gd 评级元素。因此数组 Stories2 数组只有 15 个值。所以我无法按顺序在表格视图中显示它。有没有办法检索属性标签在“找到字符”方法中。请帮帮我。谢谢。

I am new to iphone development.I am parsing an XML page and displaying the content in a tableview.In some block in the XML page , the child element is missing , i want to add a value o in my array, when the child element is not fond in the particular block.My XML file is like

 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> ..................................>first block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 </entry> ....................................>Second block
 <entry>
 <id>xxx </id>
 <title>xxxxx</title>
 <gd:ratings numRaters="2"/>
 </entry> .....................................>Third block

I am parsing the gd:ratings tag and display its attribute numRates value in a tableview. I am having an mutable array which stores the value of numRates.I have to add object "O" in the array when the second block is parsed because it doesnot contain gd:ratings tag.Since the value is present in a attribute tag, for retrieving the content and adding it to the mutable array is done in NSXML parser DidStartElement method.

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

currentElement = [elementName copy];
if ([elementName isEqualToString:@"entry"]) {

    entry = [[NSMutableDictionary alloc] init];
    currentTitle = [[NSMutableString alloc] init];
    currentDate = [[NSMutableString alloc] init];
    NSLog(@"inside image1 ");
}else if([elementName isEqualToString:@"media:thumbnail"])
    { currentImage = [[NSMutableString alloc] init];
        if(myUrl == nil){   
        myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
        }
        [currentImage appendString:myUrl];  
        [stories1 addObject:currentImage];
        myUrl=nil;
        }   else if([elementName isEqualToString:@"gd:rating"])
{
    currentRating=[[NSMutableString alloc]init];

        myRatings=[NSString stringWithString:[attributeDict objectForKey:@"numRaters"]];

    [currentRating appendString:myRatings];
    [stories2 addObject:currentRating];
}   
}

Since there are 25 blocks of entry tags and in that 10 block doesnot have gd rating element .So the array stories2 array has only 15 values.So i cannot display it in the tableview in order.Is there any way out to retrieve the attribute tag in "found characters" method . Please help me out.Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-08-27 23:19:40

您可以使用 didEndElement 方法:当外部元素结束时,检查子元素是否已添加到数组中。

您可以使用 iVar BOOL entryHasRating 并执行以下操作:

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

   currentElement = [elementName copy];
   if ([elementName isEqualToString:@"entry"]) {
     entryHasRating = NO;
     ...
   } else if ([elementName isEqualToString:@"gd:rating"]){
     entryHasRating = YES;
     ...
   }
}

然后当元素结束时:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
   if ([elementName isEqualToString:@"entry"]&& !entryHasRating) {
       [stories2 addObject:@"No Rating"];
       entryHasRating = YES; 
   }
   ...
}

You can use the didEndElement method: when the outer element ended, check if the child element was added to the array.

You could use an iVar BOOL entryHasRating and do the following:

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

   currentElement = [elementName copy];
   if ([elementName isEqualToString:@"entry"]) {
     entryHasRating = NO;
     ...
   } else if ([elementName isEqualToString:@"gd:rating"]){
     entryHasRating = YES;
     ...
   }
}

Then when an element ends:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
   if ([elementName isEqualToString:@"entry"]&& !entryHasRating) {
       [stories2 addObject:@"No Rating"];
       entryHasRating = YES; 
   }
   ...
}
腻橙味 2024-08-27 23:19:40

如果您知道所需的所有数据字段,则可以在解析 XML 之前为所有预期值构建一个包含占位符的字典;当您解析 XML 时,只需将占位符替换为实际值即可。

If you know all of the data fields that you'll be expecting, you could build a dictionary with placeholders for all of your expected values before you parse the XML; as you parse the XML just replace the placeholders with real values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文