首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hpple xpath问题

hpple xpath问题
EN

Stack Overflow用户
提问于 2013-04-08 13:11:46
回答 1查看 616关注 0票数 1

我正在使用hpple解析一个HTML文档。我学习了的教程,并将所有内容都用于他们的示例文件。但是,我需要稍微修改一下,以便为我的朋友博客读取一个特定的HTML文件。该文件比我迄今使用的示例要复杂得多。文件的相关部分(全部上载在要旨上)是:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<!-- snip -->
<div id="content" class="hfeed">
            <div class="post-21443 post type-post status-publish format-standard hentry category-about-catherine">

      <div class="postdate">
      Apr          <br />
      6            <br />
      2013         
      </div>
    <h2 class="entry-title"><a href="http://catherinepooler.com/2013/04/stampnation-live-retreat-updates/" title="StampNation LIVE Retreat Updates" rel="bookmark">StampNation LIVE Retreat Updates</a></h2>

    <div class="post-info"></div>       <div class="entry-content">
        <p><a href="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560.jpg" ><img class="aligncenter size-large wp-image-21444" alt="StampNation LIVE" src="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560-450x337.jpg" width="450" height="337" /></a></p> <p>StampNation LIVE is in full swing!  We are having a wonderful time.  I am taking a quick break from stamping and chatting to share a few photos with you.</p> <p>I think my favorite thing in getting ready for the retreat was setting up the Accessory Bar.  Each attendee received a small galvanized bucket with their fully glittered initial on it to fill up at the bar.  Awesome!</p>
<!-- snip -->

文件中有几个部分,我需要将所有的

代码语言:javascript
复制
<h2 class = "entry-title"> 

(title="StampNation实时退出更新“)在数组中。我已经成功地放置了

代码语言:javascript
复制
<div class = "entry-content"> 

通过使用XPathQuery //div[@class = 'entry-content']/p进入数组。但是,由于一个空数组,如果没有代码崩溃,我似乎无法获得标题。显然,我的XPathQuery是不正确的。这就是我试过的。

代码语言:javascript
复制
//h2[@class = 'entry-title']  (: this crashed :)

//div[@class = 'post-21443.....']//h2[@class = 'entry-title']  (: this crashed too.   ")

还有很多其他的尝试!

有人对我有什么建议吗?我研究了许多这样的答案,以及与hpple一起出现的例子,但我无法将其拼凑在一起。

更新:在Jens帮助下,我将查询更改为

代码语言:javascript
复制
  NSString \*postsXpathQueryString = @"//h2[@class = 'entry-title']/a";

这给了我一个数组,但我现在也得到了这个错误。

2013-04-08 10:26:30.604 2013 12408:11303 *终止应用程序由于异常“NSRangeException”,原因:'* -__NSArrayM objectAtIndex::索引4越界0 .3‘*第一次抛出调用堆栈:(0x210a012 0x1203e7e7e 0x20ac0b4 0x3852 0x2028fb 0x2029fb 0x1eb1bb 0x1fbb4b 0x1982b2dd 0x17176bdd 0x122d2 0x22b99c 0x33c 0x06eaf 0x2372bd 0x17fb56 0x17e66f 0x17e589 0x17d7e4 0x17d61e 0x17e3d9 01812d2 0x22b99c 0x17876f 0x8x8x5 0x0x9x9x0x20x20x0x0x0x0x0x0x0x20x9x0x0x0x20x9x0x0x20x206x0x20x206x0x20x20612 0x20x203e7e 0x2020ac0b4 0x3852 0x2028fb 0x2029ff 0x1ebf 0x1eb1bb 0x1b 0x1fb4b 0x4b 0x201012 0x1766-0x203e7)

更新2

修正错误索引超出界限时,插入一个if语句时,我reloadData。我在我的NSLog中得到了一个数组,但它并没有把它放在我的表视图中。桌面视图是空的!!但不要再坠毁了!

最后更新

它现在正在工作,Jens帮助我得到了正确的查询,然后我只需要填写表视图。我已经将数组计数设置为20,因为Ray的tut有无数个条目。我的朋友博客,只有四个!谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-08 14:48:58

问题:

您的文档包含名称空间:

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">

解决方案:

我不熟悉hpple或ObjectiveC,所以我无法验证我从香港邮政集邮中心一期上调整的代码,但它看起来是合理的。我想您所要做的就是将第一个参数更改为xpath上下文变量。

代码语言:javascript
复制
xmlXPathRegisterNs(xpathCtx, [@"xhtml" cString],[@"http://www.w3.org/1999/xhtml" cString]); 

然后,每次访问元素时都在此命名空间前缀:

代码语言:javascript
复制
//xhtml:h2[@class = 'entry-title']

如果您不想使用名称空间(而且不需要使用名称空间,因为有不同),则可以添加通配符命名空间:

代码语言:javascript
复制
//*:h2[@class = 'entry-title']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15879979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档