“为了”在 nsxmlparser 中循环?如何排列.xml 文件?
假设这是我要解析的 .xmi 文件;
<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<link1>http://galeri.uludagsozluk.com/38/apple_36298.png</link1>
<link2>http://galeri.uludagsozluk.com/38/apple_36298.png</link2>
<link3>http://galeri.uludagsozluk.com/38/apple_36298.png</link3>
<link4>http://galeri.uludagsozluk.com/38/apple_36298.png</link4>
<link5>http://galeri.uludagsozluk.com/38/apple_36298.png</link5>
</Album>
</Albums>
如何创建一个循环来为 link1 link2 link3 等创建可变字符串。我不想受到链接数量的限制。所以循环肯定是需要的。
这是我的 nsxmlparser 代码;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary
*)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"Album"]) {
album = [[NSMutableDictionary alloc] init];
self.currentName = [[NSMutableString alloc] init];
self.currentURL = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Album"]) {
[album setObject:self.currentName forKey:@"name"];
[album setObject:self.currentURL forKey:@"link1"];
[albums addObject:[album copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString
*)string{
if ([currentElement isEqualToString:@"name"]) {
[self.currentName appendString:string];
} else if ([currentElement isEqualToString:@"link1"]) {
[self.currentURL appendString:string];
} }
使用这个解析器,我只能解析link1。但我应该为“link%@”创建一个循环,解析器应该自动解析我添加的所有链接。
或者你们是否建议一种更好的方法来解析某个元素下的多个链接?安排 .xml 文件的更好方法?我对新想法持开放态度。比如:
<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<link id="1">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="2">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="3">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="4">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="5">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
</Album>
</Albums>
或者,上面的xml是可能的吗?
提前致谢。
Lets say that this is my .xmi file to be parsed;
<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<link1>http://galeri.uludagsozluk.com/38/apple_36298.png</link1>
<link2>http://galeri.uludagsozluk.com/38/apple_36298.png</link2>
<link3>http://galeri.uludagsozluk.com/38/apple_36298.png</link3>
<link4>http://galeri.uludagsozluk.com/38/apple_36298.png</link4>
<link5>http://galeri.uludagsozluk.com/38/apple_36298.png</link5>
</Album>
</Albums>
How can I create a loop to create a mutable string for link1 link2 link3 and so on. I do not want to be limited with the number of links. So loop is certainly required.
This is my nsxmlparser code;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary
*)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"Album"]) {
album = [[NSMutableDictionary alloc] init];
self.currentName = [[NSMutableString alloc] init];
self.currentURL = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString
*)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Album"]) {
[album setObject:self.currentName forKey:@"name"];
[album setObject:self.currentURL forKey:@"link1"];
[albums addObject:[album copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString
*)string{
if ([currentElement isEqualToString:@"name"]) {
[self.currentName appendString:string];
} else if ([currentElement isEqualToString:@"link1"]) {
[self.currentURL appendString:string];
} }
With this parser, I can only parse link1. But I should create a loop for "link%@" and parser should automatically parse all my links as much as I add.
Or do you guys suggest a better way to parse multiple links under a certain element? A better way to arrange .xml file? I am open to new ideas. like:
<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<link id="1">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="2">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="3">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="4">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
<link id="5">http://galeri.uludagsozluk.com/38/apple_36298.png</link>
</Album>
</Albums>
Or, above xml is possible?
Thanx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个仅运行 5 次的 for 循环。
或者
在
元素中包含 link-N 元素。You need to either create a for loop that only runs 5 times.
or
contain the link-N elements in a
<links />
element.我发现的最佳解决方案是使用ComponentSeparatedByString方法。如果将所有链接仅添加到一个元素中,解析器会将其解析为 nsstring,并且您可以从 nsstring 创建 nsarray。而且很容易,我们可以在任何我们想要的地方使用这个数组。
Best solution I found is to componentseparatedbystring method. If one adds all of links into only one element, parser will parse it as nsstring and you can create nsarray from nsstring. And very easily, we can use this array wherever we want.