0

My question is just very similar to this question Getting an attribute value in xml element

But the file is in Html not xml. The first answer given works for xml file but not html.

<form action="action_page.php">
  First name:<br>
  <input type="text" name="fistname" value="Mickey" />
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse" />
  <br><br>
  <input type="submit" value="Submit">
</form>

I have to read values inside name attribute. Thanks in advance. This question is different from the link or any other asked on StackOverflow. I have checked.

Community
  • 1
  • 1
Praveen Rana
  • 37
  • 1
  • 8

2 Answers2

2

You can use a HTML parser like jsoup with the appropriate CSS selector, then get the attribute with attr(attributeKey).

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

...
Document doc = Jsoup.parse(html);
for (Element input : doc.select("input")){
    System.out.println(input.attr("name"));
}

Output:

fistname
lastname

You can download it from here.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

You can use JSoup to parse Html elements in java. Using Document you can parse html or you can even convert HTML type into string .

Like

Document doc = 

    Jsoup.connect("http://www.javatpoint.com").get();  
                    String title = doc.title();  
                    System.out.println("title is: " + title);  

As you said u arenew to jSoup link to it website here you will find documentation on it

JSoup

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86