我想通过网络服务阅读那些通过xml文件传入的网址。
<chaneels>
<chaneel name="AAA">
<url>http://188.138.95.120:8010/</url>
</chaneel>
<chaneel name="gold">
<url>http://188.138.95.120:8020/</url>
</chaneel>
<chaneel name="BBB">
<url>http://188.138.95.120:8090/</url>
</chaneel>
<chaneel name="CCC">
<url>http://188.138.95.120:8050/</url>
</chaneel>
<chaneel name="DDD">
<url>http://209.239.127.105:8010/</url>
</chaneel>
</chaneels>
这是我从网络服务获得的XML
。然后我想分别获取这些URL。谁能告诉我一种方法呢?
答案 0 :(得分:1)
首先通过检查chaneel
,item
,currentName
,currentURL
为allData
声明的元素property
来启动解析器。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"channel"]) {
item = [[NSMutableDictionary alloc] init];
currentName = [[NSMutableString alloc] init];
currentURL = [[NSMutableString alloc] init];
[currentName appendString:[attributeDict objectForKey:@"name"]];
}
}
之后检索欲望值......
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentElement isEqualToString:@"url"]) {
[currentURL appendString:string];
}
}
之后将该值存储到此方法中带有欲望键的字典中......
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"channel"]) {
[item setObject:currentName forKey:@"Name"];
[item setObject:currentURL forKey:@"URL"];
[allData addObject:[item copy]];
}
}
在allData
中,您将获得与其名称相对应的所有网址...