开发者

Jsoup - Android - Parse info from Form data / input

I am using Jsoup in my application and I am attempting to parse informatio开发者_StackOverflow社区n from an a few input tags in order to add them to a url and post data automatically.

The portion of HTML I am attempting to parse is as follow:

<div class='theDivClass'>
    <form method="post" id="handlePurchase" name="makePurchase" action="/shop.php">
        <input type="hidden" name="ProductCode" value="A1223MN" />
        <input type="hidden" name="SystemVersion" value="3" >
        <input type="hidden" name="ProductClass" value="BOOK" />
    </form>
</div>

The desired output would be

x = A1223MN
y = 3
z = BOOK

I am halfway familiar with JSOUP in the sense that I am able to parse out text, images, and urls but for some reason this is not clicking for me.

Any help would be greatly appreciated.


You should be able to use this:

Elements hidden = doc.select("input[type=hidden]");

And then just pull the attr values from each element in hidden. I've just tried it and it seems to work as expected.

For completeness:

Map<String,String> hiddenList = new HashMap<String, String>();
Elements hidden = doc.select("input[type=hidden]");
for (Element el1 : hidden){
    hiddenList.put(el1.attr("name"),el1.attr("value");
}

Will give you a Map of all hidden input fields in the document.


Element.select("input[name=productCode]").attr("value");
Element.select("input[name=SystemVersion]").attr("value");
Element.select("input[name=ProductClass]").attr("value");


There's another way I found:

FormElement f = (FormElement) doc.select("form#handlePurchase").first();
System.out.println(f.formData());

Result:

[ProductCode=A1223MN, SystemVersion=3, ProductClass=BOOK]


Closing this question as it appears from all of the research I have done, you cannot pull data from "hidden" input types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜