how can i return all the hyperlinks in a HTML file using LibXml 2.2 (Objective C)

438 Views Asked by At

i want to return all the hyperlinks in a html. what would be the XPath Query that ?(Objective C)

1

There are 1 best solutions below

5
On

You can use next sample for your task

NSData *data = [inputString dataUsingEncoding:NSUTF8StringEncoding];    
xmlDocPtr doc = xmlReadMemory([data bytes], [data length], "", NULL, XML_PARSE_RECOVER);

xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST("//a"), xpathCtx);

if (xpathObj && xpathObj->nodesetval) {
    for (int i = 0; i < xpathObj->nodesetval->nodeNr; ++i) {
        xmlNodePtr node = xpathObj->nodesetval->nodeTab[i];
        xmlChar *hrefProp = xmlGetProp(node, BAD_CAST("href");
        xmlChar *content = xmlNodeGetContent(node);

        NSString *hrefString = [NSString stringWithUTF8String:(char *)hrefProp];
        NSString *contentString = [NSString stringWithUTF8String:(char *)content];
        xmlFree(hrefProp);
        xmlFree(content);

        NSLog(@"Anchor %@, Content: %@", hrefString, contentString);
}
xmlXPathFreeContext(xpathCtx);
xmlXPathFreeObject(xpathObj);
xmlFreeDoc(doc);

For setting up libxml2 read carefully section Setting up your project file. Also you can download working sample from site and compile.