iOS Parse HTML attribute with Hpple

折月煮酒 提交于 2019-12-12 05:37:16

问题


I am trying to retrieve an attribute in an input tag in HTML. My NSLog is coming back null. What's wrong with my syntax? I am trying to retrieve the value for "value=" in the input tag.

Heres the HTML I am parsing:

<input name="hidXMLID" type="hidden" id="hidXMLID" value="0311sg55dbmnk0nsuzoflnij" />

Heres the parser syntax:

NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];
NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];
TFHppleElement *element = [elements objectAtIndex:0];
NSString *content = [element content];

NSLog(@"ID = %@",content);

回答1:


I imagine this could be solved better with different XPath query and inspection of the attributes, something maybe like:

NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']"];
TFHppleElement *element = [elements objectAtIndex:0];
NSDictionary *attributes = [element attributes];
NSString *idValue = [attributes objectForKey:@"id"];

From your post and your logs, I think that this may work without changing the query:

TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];
NSString *idValue = [child content];


来源:https://stackoverflow.com/questions/12184357/ios-parse-html-attribute-with-hpple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!