NSXMLDocument,使用nodesForXPath搜索: [英] NSXMLDocument, search with nodesForXPath:

查看:59
本文介绍了NSXMLDocument,使用nodesForXPath搜索:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在HTML文档中搜索可可中两个特定的文本字符串.我正在使用以下网页创建NSXMLDocument:页面示例然后,我试图在其中搜索应用程序标题和图标的网址.我目前正在使用此代码搜索特定的字符串:

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;
    }

当我尝试搜索这些字符串时,出现错误:"XQueryError:3-"无效令牌(@)-./*/div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='title'@ class ='intro has-gcbadge']/h1行:1"

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"

我对此教程较为宽松

我也尝试了在xPath中没有所有@符号的情况,并且还返回了错误.对于xPath,我的语法显然是错误的.该路径的基本语法是什么.我看过很多带有基本XML树的示例,但没有html.

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.

推荐答案

我怀疑那是附近然后是您要测试两个属性的部分

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";

尝试将其更改为:

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

这篇关于NSXMLDocument,使用nodesForXPath搜索:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆