سؤال

my XML page look like this:

<announcement>
  <body>

... then many tags follow

<files>
  <images originalSize="101554" id="4641530">
    <description>Ласковый покалеченный приютский пёс Федя в дар.</description>
    <image id="21126492" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126492.jpg" name="first" width="56" height="56"/>
    <image id="21126493" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126493.jpg" name="prev" width="103" height="77"/>
    <image id="21126494" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126494.jpg" name="prev_card" width="158" height="124"/>
    <image id="21126495" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126495.jpg" name="middle" width="462" height="348"/>
    <image id="21126496" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126496.jpg" name="default" width="640" height="482"/>
  </images>
  <images originalSize="146409" id="4641531">
    <description>Ласковый покалеченный приютский пёс Федя в дар.</description>
    <image id="21126497" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126497.jpg" name="first" width="56" height="56"/>
    <image id="21126498" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126498.jpg" name="prev" width="103" height="77"/>
    <image id="21126499" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126499.jpg" name="prev_card" width="158" height="124"/>
    <image id="21126500" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126500.jpg" name="middle" width="284" height="348"/>
    <image id="21126501" url="http://dmir.ru/images/laskovyy-pokalechennyy-priyutskiy-pes-fedya-v-dar-21126501.jpg" name="default" width="554" height="680"/>
  </images>

...other 3 tags with image, then closing tag <body>

There is about 40 similar XML elements with image, text tags and rest, i don't want to put it all here because its quite large.

My point is, i can easy parse each element like that:

if ([elementName isEqualToString:@"text"]), then save it to an array, but, there is a lot image tags. When i can easy find corresponding text or description tag, i can't find proper image, because there is a lot of them. I want to get an image with proper width/height (one from 30), and then save it and move further, but now the best i did is have array of unsorted image links like this:

if([elementName isEqualToString:@"image"]){

        self.imageString = [attributeDict objectForKey:@"url"];
        if (self.imageString) {
//        NSLog(@"%@", self.imageString);
        }

       [self.listOfImages addObject:self.imageString];

Ohh, i really hope you here with me and now u get my point and what i want to do. How can i get proper image string (one from many) that correspond to proper key (when i tried to use keys i found that solution not working, because each element contain 5 keys, for example i want single element with key "url", which further have key "prev", and also want only one object with that parameters).

Any help or advice would be appreciated. Thank you!

Edited: i tried to compare two strings like this, maybe that would help to figure out correct string:

if ([elementName isEqualToString:@"images"]){

        descriptionNew = [self.currentPosition objectForKey:@"description"];

        }


    if (descriptionOld == descriptionNew){
        NSLog(@"equal");
    }   else {
        newElement = YES;
        NSLog(@"not equal");
    }

Then BOOL newElement could be used to remove last object from an array.. but i think that approach not very good, solution should be simpler

هل كانت مفيدة؟

المحلول

You said:

when i tried to use keys i found that solution not working, because each element contain 5 keys, for example i want single element with key "url", which further have key "prev", and also want only one object with that parameters

You could accomplish that with something like:

if ([elementName isEqualToString:@"image"])
{
    NSString *name = attributDict[@"name"];

    if ([name isEqualToString:@"prev"] && self.imageURLString == nil) 
    {   
        NSString *urlString = attributeDict[@"url"];
        if (urlString)
        {
            self.imageURLString = urlString;
        }
    }
}

Elsewhere, though, you said:

When i can easy find corresponding text or description tag, i can't find proper image, because there is a lot of them. I want to get an image with proper width/height (one from 30), and then save it and move further ...

You'd have to describe what rules you want to apply to determine the "proper width/height". You could probably just want to have the parser save each of the image the whole attributeDict:

if ([elementName isEqualToString:@"image"])
{
    [self.listOfImages addObject:attributeDict];
}

You could then, when you're done parsing all of the images, iterate through all of these and identify which is "best" and prune the rest away if you want. You could also do this as you're parsing (e.g. save the attributeDict of the "best image thus far", and every time you parse a new one, see if the dimensions are better, and if so, replace the "best image" with the current one).

But without knowing the precise rules you want to apply, it's hard to be more specific than that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top