iosnsxmlparsernsxmlparserdelegate

Duplicate data while NSXMLParser reading xml-file


I've such xml-file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <background>assets/_image.png</background>
    <items>
        <item type='test1' position='6' x='123' y='456'>
            my_way
        </item>
        <item type='test2' position='8' x='456' y='123'>
            another_way
        </item>

.........................

I read it using NSXMLParser:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([self.currentTag isEqualToString:@"item"]) {
        [self addItemForAttributes:self.currentAttributes withValue:string];

        NSLog(@"%@", self.items.lastObject.type);
        NSLog(@"%@", self.items.lastObject.position);
        NSLog(@"%@", NSStringFromCGPoint(self.items.lastObject.coordinate));
        NSLog(@"%@", self.items.lastObject.value);
    }
}

I write item into items array. But when I read it in console, some items looks as duplicated.

2019-07-02 17:25:53.326939+0300 Game[1280:235820] test1
2019-07-02 17:25:53.327083+0300 Game[1280:235820] 6
2019-07-02 17:25:53.327445+0300 Game[1280:235820] {123, 456}
2019-07-02 17:25:53.327671+0300 Game[1280:235820] my_way
2019-07-02 17:25:53.327946+0300 Game[1280:235820] test1
2019-07-02 17:25:53.328021+0300 Game[1280:235820] 6
2019-07-02 17:25:53.328301+0300 Game[1280:235820] {123, 456}
2019-07-02 17:25:53.328348+0300 Game[1280:235820] 
2019-07-02 17:25:53.328991+0300 Game[1280:235820] test1
2019-07-02 17:25:53.329112+0300 Game[1280:235820] 6

How to fix it?


Solution

  • Delegate method(found characters) calls several times, I made condition for it(filter empty strings), so it solved my problem.