如何可以轻松地提取字段名称和值在表单上 [英] How can i extract field name and values on a form easily

查看:236
本文介绍了如何可以轻松地提取字段名称和值在表单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析字段名称和值从html表单添加到我的数据库。我知道我可以去,做一个找到
输入名称='然后开始另一个查找找到结束',并获得数据通过中间函数,然后做同样
的值通过find value ='
我想知道是否有一个更简单的方法来循环文档和提取所有输入名称和相关联的值?

I am in need of parsing field name and values from an html form to add to my db. I know i can go and do a find "input name='" then start another find to find the closing "'" and get the data via mid function then do the same for value via find "value='" I was wondering if there is an easier way to loop the doc and extract all input names and the associated values ?

下面是我要解析的页面的样例:

Below is a sample of what my page to parse looks like

    <input name='a_glare' value='B' class='inputbox-highlighted-false' size='1' maxlength='1'> 
</td> 
<td align="center"> 
    <input name='a_testani' value='' class='inputbox-highlighted-false' size='1' maxlength='1'> 
</td> 
<td align="center"> 
    <input name='a_tksig' value='EC' class='inputbox-highlighted-false' size='2' maxlength='2'> 
</td> 
<td align="center"> 
    <input name='a_sacnon' value='' class='inputbox-highlighted-false' size='1' maxlength='1'> 
</td> 
<td align="center"> 
    <input name='a_ot' value='' class='inputbox-highlighted-false' size='1' maxlength='1'> 
</td> 
<td align="center"> 
    <input name='a_ovlp' value='' class='inputbox-highlighted-false' size='1' maxlength='1'> 


推荐答案

对于解析html,我建议使用 JSoup 而不是正则表达式。我刚开始使用JSoup,发现它非常简单的使用。只需下载该jar并将其添加到您的应用程序类路径

For parsing html, I would recommend using JSoup instead of regular expressions. I just started using JSoup and found it extremely simple to use. Just download the jar and add it to your application class path.

我不是任何方式的专家,但可以使用以下代码片段打印您的示例html页面中的所有输入字段:

I am not an expert by any means, but was able to print all of the "input" fields from your sample html page using this snippet:

<cfscript>
    // parse html string into document
    jsoup = createObject("java", "org.jsoup.Jsoup");
    doc = jsoup.parse( yourHTMLContentString );

    // grab all "input" fields
    fields = doc.select("input");

    for (elem in fields) {
        // get attributes of each field
        fieldName = elem.attr("name");
        fieldValue = elem.attr("value");
        fieldType = elem.attr("type");

        // display values
        WriteOutput("<br>type: "& fieldType 
              &" name: "& fieldName 
              &" value: "& fieldValue
        );
    }

</cfscript>

(..和是,尽管你的名字,我 JSoup4You)

(.. and yes, despite your moniker, I am suggesting "JSoup4You" )

更新

字段变量是一个数组。所以你可以循环通过它在cfml中以同样的方式。看起来像双重工作,但如果你喜欢,你可以提取输入名称和值到你自己的结构数组(或任何CF结构你喜欢)。例如:

The fields variable is an array. So you can loop through it in cfml the same way. It seems like double work, but if you prefer, you can extract the input names and values into your own array of structures (or whatever CF construct you like). For example:

// initialize storage array
yourArray = [];

for (elem in fields) {

    // extract field properties into a structure 
    data = { name=elem.attr("name")
            , value=elem.attr("value")
            , type=elem.attr("type")
    };

    // store in array
    arrayAppend(yourArray, data);
}

// display array contents
WriteDump(yourArray);

这篇关于如何可以轻松地提取字段名称和值在表单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆