java - get all attributes in html string in jsoup -
java - get all attributes in html string in jsoup -
i have string in html format , trying attributes , values using jsoup.
string is
string string= "<button class=submit btn primary-btn flex-table-btn js-submit type=submit>sign in</button>"; document doc = jsoup.parse(string); seek { org.jsoup.nodes.attributes attrs = doc.attributes(); for( org.jsoup.nodes.element element : doc.getallelements() ) { for( attribute attribute : element.attributes() ) { system.out.println( attribute.getkey() + " --::-- "+attribute.getvalue() ); } } } grab (exception e) { e.printstacktrace(); }
my desired output ::
key: **class** , value is: **submit btn primary-btn flex-table-btn js-submit** key: **type** , value is: **submit**
but this
key: class , value is: submit key: btn , value is: key: primary-btn , value is: key: flex-table-btn , value is: key: js-submit , value is: key: type , value is: submit
this because of quotes. if utilize
string string= "<button class='submit btn primary-btn flex-table-btn js-submit' type='submit'> sign in</button>";
i desired output.but trying without quotes.
you can't without quotes, because quotes not optional. without quotes, html you've quoted describes element one class (submit
) , series of non-class, invalid additional attributes names btn
, flex-table
, etc., , that's how browser interpret it, jsoup doing. if meant additional classes on element, quotes required.
from the specification:
unquoted attribute value syntax
the attribute name, followed 0 or more space characters, followed single u+003d equals sign character, followed 0 or more space characters, followed attribute value, which, in add-on requirements given above attribute values, must not contain literal space characters, u+0022 quotation mark characters ("), u+0027 apostrophe characters ('), "=" (u+003d) characters, "<" (u+003c) characters, ">" (u+003e) characters, or u+0060 grave accent characters (`), , must not empty string.
note "must not contain literal space characters" part i've emphasized.
java jsoup
Comments
Post a Comment