“为了”在 nsxmlparser 中循环?如何排列.xml 文件?

发布于 2024-12-03 19:03:51 字数 2595 浏览 0 评论 0原文

假设这是我要解析的 .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 技术交流群。

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

发布评论

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

评论(2

|煩躁 2024-12-10 19:03:51

您需要创建一个仅运行 5 次的 for 循环。

或者

元素中包含 link-N 元素。

<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<links>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>    
</links>
</Album>
</Albums>

You need to either create a for loop that only runs 5 times.

or

contain the link-N elements in a <links /> element.

<?xml version="1.0" encoding="UTF-8"?>
<Albums>
<Album id="1">
<name>Images</name>
<links>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>
    <link>http://galeri.uludagsozluk.com/38/apple_36298.png</link>    
</links>
</Album>
</Albums>
沫离伤花 2024-12-10 19:03:51

我发现的最佳解决方案是使用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.

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