Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSXMLDocument, search with nodesForXPath:

I need to search through an HTML document for two specific strings of text in cocoa. I am creating an NSXMLDocument with the web page: Page Example Then I am trying to search it for the app title, and the url of the icon. I am currently using this code to search for the specific strings:

NSString *xpathQueryStringTitle = @"//div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='title' @class='intro has-gcbadge']/h1";
NSString *xpathQueryStringIcon = @"//div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='left-stack']/div[@class='lockup product application']/a";
NSArray *titleItemsNodes = [document nodesForXPath:xpathQueryStringTitle error:&error];
if (error)
    {
        [[NSAlert alertWithError:error] runModal];
        return;
    }
error = nil;
NSArray *iconItemsNodes = [document nodesForXPath:xpathQueryStringIcon error:&error];
    if (error)
    {
        [[NSAlert alertWithError:error] runModal];
        return;
    }

When I try to search for these strings I get the error: "XQueryError:3 - "invalid token (@) - ./*/div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='title' @class='intro has-gcbadge']/h1" at line:1"

I am loosely following this tutorial.

I also tried this without all of the @ symbols in the xPath, and it also returns an error. My syntax is obviously wrong for the xPath. What would the basic syntax be for this path. I've seen plenty of examples with a basic XML tree, but not html.

like image 328
Brandon Mcq Avatar asked Jul 13 '26 01:07

Brandon Mcq


1 Answers

I suspect it's that part near then end where you have a test for two attributes

/div[@id='title' @class='intro has-gcbadge']/h1";

Try changing it to:

/div[@id='title'][@class='intro has-gcbadge']/h1";
like image 97
Steven D. Majewski Avatar answered Jul 16 '26 05:07

Steven D. Majewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!