0

I need to get an image and its title from html string. I heard, javascript may get elements via tags or ids, but I've never used javascript earlier. So I found a nice framework - JavaScriptCore and here's my code snippet:

JSContext* context = [[JSContext alloc] initWithVirtualMachine:[JSVirtualMachine new]];
NSString* jsString = [NSString stringWithFormat:@"var parser = new DOMParser();"
                          "var doc = parser.parseFromString(%@, \"text/html\");"
                          "var imgs = document.getElementsByTagName('img');", [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@""]];
[context evaluateScript:jsString];

But context[@"parser"], context[@"doc"] and context[@"imgs"] return undefined. What do I make wrong? Maybe I will be better to use some other approach without JavaScriptCore?

1 Answer 1

1

You can use some third party library that supports XPath queries. IMHO that would be a better way to work with DOM.

Here's a link with example http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

Sign up to request clarification or add additional context in comments.

3 Comments

Your link use NSXMLDocument which is only of OS X
@ValentinShamardin I'm sorry, my bad. I've misread the question. Updated an answer. Thank you for pointing it out
Yes, I saw this link, thank you. Thank you for your time, I vote up.

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.