如何在iphone编程中使用nsxmlparser解析多个节点同名的xml?

发布于 2024-12-07 09:38:26 字数 672 浏览 0 评论 0原文

如果我需要所有图像,我如何使用 NSXMLParser 进行解析,

<title>Title</title>
<description>my description is here </description>

<images>

<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.7218.jpg</image>

<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.mounted.jpg</image>

<image>http://www.sosmoths.com/mothImages/800px-XN_Tineola_bisselliella_1.jpg</image>

<image>http://www.sosmoths.com/mothImages/Tineola_bisselliella.JPG</image>

</images>

有更多的节点,如标题、描述,所以如何解析这样的 xml,我对图像节点感到困惑,我正在考虑使用 NSMutableArray 来实现此目的,但仍然不清楚做代码吗?

How can I parse using NSXMLParser if I need all the images,

<title>Title</title>
<description>my description is here </description>

<images>

<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.7218.jpg</image>

<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.mounted.jpg</image>

<image>http://www.sosmoths.com/mothImages/800px-XN_Tineola_bisselliella_1.jpg</image>

<image>http://www.sosmoths.com/mothImages/Tineola_bisselliella.JPG</image>

</images>

There are more nodes like title, description, so how to parse such xml, I am confused about image nodes, I am thinking to use NSMutableArray for this, but still not clear to do code?

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

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

发布评论

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

评论(3

残龙傲雪 2024-12-14 09:38:26
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
                                  namespaceURI:(NSString *)namespaceURI
                                 qualifiedName:(NSString *)qName

{

if ([elementName isEqualToString:"<image>"])
        {
            [imagearray addObject:imageurl];

        }

}

在此之前,您需要找到元素名称,然后执行上面的代码。这可能会对您有所帮助。

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

{

if ([elementName isEqualToString:"<image>"])
        {
            [imagearray addObject:imageurl];

        }

}

Before this you need to find the element name is and then do above code.This may help you.

调妓 2024-12-14 09:38:26
use this it work

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    NSLog(@"%@",elementName);
    if([elementName isEqualToString:@"moths"]){
        mytblarray=[[NSMutableArray alloc] init];
    } else if([elementName isEqualToString:@"moth"]){
        tmpdic=[[NSMutableDictionary alloc] init];
    }
    else if([elementName isEqualToString:@"images"]){
        imgArray=[[NSMutableArray alloc] init];
    }

}

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

    if(tmpstr!=nil && [tmpstr retainCount]>0){ [tmpstr release]; tmpstr=nil; }
    tmpstr=[[NSString alloc] initWithString:string];

}

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

    if([elementName isEqualToString:@"moth"]){
        [mytblarray addObject:tmpdic];
        [tmpdic release];
    }if([elementName isEqualToString:@"images"]){
        [tmpdic setValue:imgArray forKey:elementName];
    }
    else if([elementName isEqualToString:@"image"]){
        [imgArray addObject:tmpstr];        
    }
}
use this it work

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    NSLog(@"%@",elementName);
    if([elementName isEqualToString:@"moths"]){
        mytblarray=[[NSMutableArray alloc] init];
    } else if([elementName isEqualToString:@"moth"]){
        tmpdic=[[NSMutableDictionary alloc] init];
    }
    else if([elementName isEqualToString:@"images"]){
        imgArray=[[NSMutableArray alloc] init];
    }

}

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

    if(tmpstr!=nil && [tmpstr retainCount]>0){ [tmpstr release]; tmpstr=nil; }
    tmpstr=[[NSString alloc] initWithString:string];

}

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

    if([elementName isEqualToString:@"moth"]){
        [mytblarray addObject:tmpdic];
        [tmpdic release];
    }if([elementName isEqualToString:@"images"]){
        [tmpdic setValue:imgArray forKey:elementName];
    }
    else if([elementName isEqualToString:@"image"]){
        [imgArray addObject:tmpstr];        
    }
}
北陌 2024-12-14 09:38:26
  1. 在 initWithData 中采用字典: methode
  2. 将值作为键,如
  3. didStartElement 方法中的标题、描述
    ,在图像标记中分配 image_array
  4. 在 didEndElement 方法中,在图像标记中将您的数据(此处是您的图像链接)添加到 image_array
  5. 现在在 didEndElement 方法中,在图像标签中将该数组作为关键图像添加到您的主字典中,

仅此而已......

  1. take dictionary in initWithData: methode
  2. Take values as key like title, description
  3. in didStartElement method, in images tag alloc image_array
  4. in didEndElement method, in image tag add your data(here your image link) to image_array
  5. now in didEndElement method, in images tag add that array to your main dictionary as key images

that's all.....

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