4

I'm parsing an xml file and I can NSLog the parsing, but my problem is that I need to get the image url`s from this "string":

<p>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg"><img class="alignnone size-thumbnail wp-image-81" title="ex4" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg"><img class="alignnone size-thumbnail wp-image-80" title="ex3" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg"><img class="alignnone size-thumbnail wp-image-79" title="ex2" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg"><img class="alignnone size-thumbnail wp-image-71" title="ex1" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg" alt="" width="150" height="150" /></a>
 </p>

Sorry for the plain code :)

what im using to extract the url´s is this code but its not working:

   NSRange start = [item.imageGallery       rangeOfString:@"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/"];
    NSRange end = [item.imageGallery rangeOfString:@"\" "];

    int rangeLength = (int)(end.location - start.location);

    NSString *hrefString = [[NSString alloc] initWithString:[item.imageGallery substringWithRange:NSMakeRange(start.location, rangeLength)]];
    NSLog(@"image url = %@",hrefString);
3
  • 1
    you need to post an example of the xml or something if you want people to help you. This is akin to saying "my car won't start. how do i fix it?" Commented Feb 14, 2012 at 14:03
  • Well, you can start by posting the format of the xml file and your parsing code so we can actually help you. Commented Feb 14, 2012 at 14:04
  • Try using a regular expression to filter out the URL's Commented Feb 14, 2012 at 14:06

2 Answers 2

7

Using a regular expression: "src=\"([^\"]+)\""

Here is some example code:

NSString *searchedString = @""  
    @"<p>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg\"><img class=\"alignnone size-thumbnail wp-image-81\" title=\"ex4\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg\"><img class=\"alignnone size-thumbnail wp-image-80\" title=\"ex3\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg\"><img class=\"alignnone size-thumbnail wp-image-79\" title=\"ex2\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg\"><img class=\"alignnone size-thumbnail wp-image-71\" title=\"ex1\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"</p>";
NSRange rangeOfString = NSMakeRange(0, [searchedString length]);
//NSLog(@"searchedString: %@", searchedString);

NSString *pattern = @"src=\"([^\"]+)\"";
NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matchs = [regex matchesInString:searchedString options:0 range:rangeOfString];
    for (NSTextCheckingResult* match in matchs) {
        NSLog(@"url: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
    }

NSLog Output:

url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg
Sign up to request clarification or add additional context in comments.

Comments

2

here, I found it for you:

https://stackoverflow.com/a/5999294/1047258

The code from that answer:

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];

Then to handle the URL(s):

for (NSTextCheckingResult *match in matches) {
        NSURL *url = [match URL];
        // do whatever you want with the url
}

2 Comments

I have tested it and it wont print a clean url it prints this: 2012-02-14 14:33:28.685 Excellens[37314:17003] url = <NSLinkCheckingResult: 0x87bab60>{12, 76}{bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/…} 2012-02-14 14:33:28.686 Excellens[37314:17003] url = <NSLinkCheckingResult: 0x87b9730>{157, 84}{bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/…} 2012-02-14 14:33:28.687 Excellens[37314:17003] url = <NSLinkCheckingResult: 0x87b6450>{257, 3}{tel:150}
The matches are from type NSTextCheckingResult. Use [match URL] to get an NSURL object from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.