1

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.

2 Answers2

0

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>
Valamas
  • 24,169
  • 25
  • 107
  • 177
  • How can I create NSArray from this links element, then? – Yasin Babahanoglu Sep 06 '11 at 23:59
  • Images http://galeri.uludagsozluk.com/38/apple_36298.png http://galeri.uludagsozluk.com/38/apple_36298.png http://galeri.uludagsozluk.com/38/apple_36298.png http://galeri.uludagsozluk.com/38/apple_36298.png http://galeri.uludagsozluk.com/38/apple_36298.png how about adding an attribute? this may work? – Yasin Babahanoglu Sep 07 '11 at 21:51
  • Generally you would contain your links in a ``. As for the syntax NSArray, sorry I can't help you on that. – Valamas Sep 07 '11 at 22:12
  • I found the following SO articles to make up for my half answer. I hope they help. http://stackoverflow.com/questions/992901/how-do-i-iterate-over-an-nsarray. Here is another that shows loops. http://forums.macrumors.com/showthread.php?t=577363 Could give you some ideas. – Valamas Sep 07 '11 at 22:14
0

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.