HELP HELP PLEASE HELP

Discussion in 'Ruby' started by beth, Oct 27, 2005.

  1. beth

    beth Guest

    i have written a slighly bastardized version of the class web page
    which is given as an example of how to use the html tokenizer.

    What I've written is:

    def parse(host)
    dict = { }
    @body= host.getHTML()
    if !@body
    return
    end
    theTags = ['select','input']
    for x in theTags do
    tokenizer = HTMLTokenizer.new(@body)
    while tag = tokenizer.getTag(x)
    name = tag.attr_hash['name']
    type = tag.attr_hash['type']
    if name != nil then
    dict[type]=name
    end # if
    end# while
    end# for x
    return dict
    end # end parse

    My problem is that parse will return all tag attributes of "type" (ie
    "checkbox, "button", "hidden") except for those which are of type
    "text". I haven't been able to figure out where I'm going wrong.

    Beth
     
    beth, Oct 27, 2005
    #1
    1. Advertisements

  2. Have you made sure your text input tags have a name attribute?

    require 'html/htmltokenizer'

    host =3D Object.new
    def host.getHTML
    html =3D <<EOT
    <form>
    <input name=3D"txt1" type=3D"text" value=3D"test" />
    <input name=3D"pwd1" type=3D"password" value=3D"test" />
    <input name=3D"ck1" type=3D"checkbox" value=3D"1" checked=3D"true"/>
    </form>
    EOT
    end

    # Your code follows unchanged

    def parse(host)
    dict =3D { }
    @body=3D host.getHTML()
    if !@body
    return
    end
    theTags =3D ['select','input']
    for x in theTags do
    tokenizer =3D HTMLTokenizer.new(@body)
    while tag =3D tokenizer.getTag(x)
    name =3D tag.attr_hash['name']
    type =3D tag.attr_hash['type']
    if name !=3D nil then
    dict[type]=3Dname
    end # if
    end# while
    end# for x
    return dict
    end # end parse

    p parse(host)

    -- Output --
    {"text"=3D>"txt1", "checkbox"=3D>"ck1", "password"=3D>"pwd1"}

    Regards,

    Sean
     
    Sean O'Halpin, Oct 27, 2005
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.