سؤال

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);
هل كانت مفيدة؟

المحلول

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];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top