Bizouris
Bizouris

Reputation: 31

Get all Html5 Tags using Jsoup?

Is there a way to get all html5tags with one command instead of doing this? :

int h = (doc.getElementsByTag("canvas").size()
                    + doc.getElementsByTag("audio").size()
                    + doc.getElementsByTag("embed").size()
                    + doc.getElementsByTag("source").size()

                        etc.

Upvotes: 1

Views: 574

Answers (2)

Alex
Alex

Reputation: 3257

Here you are :

all_html5_tags = [ '!DOCTYPE' ,'a' ,'abbr' ,'acronym' ,'address' ,'applet' ,'area' ,'article' ,'aside' ,'audio' ,'b' ,'base' ,'basefont' ,'bdi' ,'bdo' ,'big' ,'blockquote' ,'body' ,'br' ,'button' ,'canvas' ,'caption' ,'center' ,'cite' ,'code' ,'col' ,'colgroup' ,'command' ,'datalist' ,'dd' ,'del' ,'details' ,'dfn' ,'dir' ,'div' ,'dl' ,'dt' ,'em' ,'embed' ,'fieldset' ,'figcaption' ,'figure' ,'font' ,'footer' ,'form' ,'frame' ,'frameset' ,'h1' ,'h2' ,'h3' ,'h4' ,'h5' ,'h6' ,'head' ,'header' ,'hgroup' ,'hr' ,'html' ,'i' ,'iframe' ,'img' ,'input' ,'ins' ,'kbd' ,'keygen' ,'label' ,'legend' ,'li' ,'link' ,'map' ,'mark' ,'menu' ,'meta' ,'meter' ,'nav' ,'noframes' ,'noscript' ,'object' ,'ol' ,'optgroup' ,'option' ,'output' ,'p' ,'param' ,'pre' ,'progress' ,'q' ,'rp' ,'rt' ,'ruby' ,'s' ,'samp' ,'script' ,'section' ,'select' ,'small' ,'source' ,'span' ,'strike' ,'strong' ,'style' ,'sub' ,'summary' ,'sup' ,'table' ,'tbody' ,'td' ,'textarea' ,'tfoot' ,'th' ,'thead' ,'time' ,'title' ,'tr' ,'track' ,'tt' ,'u' ,'ul' ,'var' ,'video' ,'wbr' ]

Upvotes: 1

ashatte
ashatte

Reputation: 5538

You can achieve the same result in one command using the select() method. This method allows you to specify multiple selectors, and Jsoup will return unique elements from the document that match any of the specified selectors.

For example:

int h = doc.select("canvas, audio, embed, source").size(); 

You can add as many comma-separated arguments as you need (e.g. all of the new elements introduced in HTML5).

Upvotes: 1

Related Questions