Hardik Lotiya
Hardik Lotiya

Reputation: 370

jsoup getElementsByAttribute Issue

here input code

<table style=”padding: 0px; margin: 0px;” cellpadding="0" cellspacing="0" width="626" align="center" bgcolor="White" border="0">
<tr>
<td style=”vertical-align: top;” height="61" valign="top" bgcolor="#0492CB"><a href="http://www.aoec.com"> <img alt="AoEC" src="http://www.aoec.com/email/mute/images/header.gif" style="width: 626px; height: 61px;" border="0" /></a></td>
</tr>
</table>

java coding is

Document doc = Jsoup.parse(code);
Elements elements = doc.getElementsByAttribute("style");
for(int se=0;se<elements.size();se++)
{
 System.out.println(elements.get(se).attr("style"));
}

out-put is

padding:;
vertical-align:;

In above code getElementsByAttribute("style") is not working..

Upvotes: 2

Views: 6329

Answers (2)

Igor Lobo
Igor Lobo

Reputation: 21

I think this is deprecated already. You should use .select(); method. Also, it is a good idea to use foreach or iterator while looping through it, as a simple for may throw some NullPointerExceptions

You can find it's documentation here

But as of not I'd use that:

//That should get you all tags that have the style attribute
Elements elements = doc.select("[style]");

//That foreach should avoid exceptions, and loop through the collection 
for(Element element : elements) {
    //The print gets a little cleaner too!
    System.out.println(element.attr("style"));
}

Upvotes: 1

BalusC
BalusC

Reputation: 1108912

The curly quotes are not valid attribute separators. You need straight quotes instead: " or '.

Your code is fine. The input HTML is just invalid. This has to be reported to the original HTML author so that s/he can fix the HTML (it would namely also break in a normal webbrowser).

If this HTML is outside your control, consider using String#replace() to replace the invalid quotes.

code = code.replace('”', '"');

Or, if the character encodings in your environment are not already properly configured to UTF-8.

code = code.replace('\u201D', '"')

Upvotes: 2

Related Questions