0

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);
8
  • Is only content nil? Or is one of element, elements, parser, or data nil also? Commented Aug 29, 2012 at 19:15
  • Only content is nil. When I put NSlog to read element and elements, they have the value in there, but also with a lot of other junk. Commented Aug 29, 2012 at 19:18
  • Would you mind posting those logs? Commented Aug 29, 2012 at 19:23
  • Which ones do you want? After each step? Commented Aug 29, 2012 at 19:27
  • What does element look like? And what does [element attributes] look like? Commented Aug 29, 2012 at 19:28

1 Answer 1

3

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, the bottom worked perfectly. It returned: 2012-08-29 12:53:18.435 FireCom[77864:11603] value = btpmzx452x15vqqcmkgzatux. Exactly what I needed. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.