Finding All Elements within a P tag using Xpath in iOS
I don't really think the iOS part of this question really matters too much, but I just thought I would add that part just to be safe. I'm trying to parse through some HTML for an App (the client wants to parse the HTML instead of using a Web View) and I'm using the hpple code I found from another question along with the XPath format for searching. I'm pretty comfortable with Xpath, never really used it but I'm starting to understand it but I'm stuck on trying to ge开发者_StackOverflowt one part of the HTML (I'm able to search and find everything else I need). From the source HTML file what I need to pull is enclosed in a p tag, but within the p tag there could be several other nodes as well (such as a href', em, br", etc). The whole p tag that I want is also enclosed within a div class, and I've tried to see if I can pull all elements under the div class, as well as within the p but so far I can't seem to get what I need. Below is a quick sample of how the HTML would look.
<div class='post'><p> This is some sample text that also includes a <a href="www.link.com">Sample Link</a> and also some <em>formatted</em> text.<p><p> A New line may also be present</p></div>
So what I really need to get is all <p>
tags within the div class since in some cases there could be more. I've tried //div[@class='body']/p[text()]
in my latest attempt but that didn't work either, it just pulled a period from the post.
Once I get everything I want to convert the entire post to a NSString that will be used in the App, but that part I can pretty much do on my one once I get the XPath search format correct. Thanks.
EDIT: Here is the code in iOS that I'm using to first perform the Xpath search and then turn the result into a string. Maybe it's in here that I'm doing something wrong.
NSArray *foundPosts = [xpathParser search:@"//div[@class='post']//p//node()"];
TFHppleElement *postelement = [foundPosts objectAtIndex:0];
NSString *Posts = [postelement content];
The XPath:
//div[@class='post']//p
will return all descendant p
elements under any div
tag with attribute class
equal to "post".
The XPath
//div[@class='post']//p//node()
will return all descendant nodes (texts and elements) under all the descendant p
tags of that specific div
.
精彩评论