开发者

"for" loop within nsxmlparser? how to arrange .xml file?

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:

开发者_如何学JAVA
<?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.


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜