iabudiab / HTMLKit

An Objective-C framework for your everyday HTML needs.
MIT License
239 stars 27 forks source link

Help in retrieving the attributes of a node #3

Closed poojagaonkar closed 7 years ago

poojagaonkar commented 7 years ago

Hello,

First of all, a very nice library you have created here. I am using it for a feature in my app where I need to load a HTML string , select node with particular tags and fetch the attributes of name and value from those tags. I have reached this far :

HTMLParser *parser = [[HTMLParser alloc]initWithString : htmlString];
HTMLElement *htmlElement = [[HTMLElement alloc] initWithTagName:@"//input[@type = 'hidden']"];
NSArray *nodes = [parser parseFragmentWithContextElement : htmlElement];

for(HTMLNode *node in nodes)
{
   // Here I want to have the attributes of the node 
  // e.g. something like node.Attributes["name"].Value and  node.Attributes["value"].Value

}

It would be great if you could help me out and guide me in the right direction as I am quite new to iOS. Thank you.

iabudiab commented 7 years ago

@poojagaonkar Hey, I see that you've closed the issue already. Do you still need help with your problem? Will be glad to help 😉

poojagaonkar commented 7 years ago

Hey,

Thanks for replying. I couldn't solve the issue, but could continue without using the feature for the time being.

It would be nice if you could still help me solve it if I need to implement it in future.

Thanks again :)

Pooja

On Feb 11, 2017 5:20 PM, "Iskandar Abudiab" notifications@github.com wrote:

@poojagaonkar https://github.com/poojagaonkar Hey, I see that you've closed the issue already. Do you still need help with your problem? Will be glad to help 😉

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/iabudiab/HTMLKit/issues/3#issuecomment-279139090, or mute the thread https://github.com/notifications/unsubscribe-auth/AHfiLJDBJgl_tYuX3m61f8jRtEdV0071ks5rbaBpgaJpZM4L-DeY .

iabudiab commented 7 years ago

Hey, no problem. First things first, I'm not really sure what you are trying to accomplish and why you are using the fragment parsing, so here is my interpretation:

// First you parse the htmlString into a document
HTMLDocument *doc = [HTMLDocument documentWithString:htmlString];
// Then you qeury the document for all input elements that are hidden
NSArray *hiddenInputs = [doc querySelectorAll:@"input[type=hidden]"];

// Now you iterate the elements array
for (HTMLElement *element in hiddenInputs) {
    NSLog(@"%@: %@", element.attributes[@"name"], element.attributes[@"value"]);
}

You couldn't access the attributes in your case because you were iterating an array of HTMLNodes. A HTMLNode can be any type of DOM node, e.g. a text node or comment node, but only HTMLElements have attributes.

So in your case you have to check the node's type and cast accordingly. something like this:

for (HTMLNode *node in nodes) {
    if (node.nodeType != HTMLNodeElement) {
        continue;
    }
    HTMLElement *element = (HTMLElement *)node;
    // You logic here
}

Let me know if this answers your questions.