문제

I am using the following code to extract all alt tags from an html file:

Elements imgs =  doc.select("img");

                    for (Element alts : imgs) {

                        String altText = alts.attr("alt");


                        if(!(alts.attr("alt").equals(null)))
                            System.out.println("alt tags: " + alts.attr("alt"));
                        //getGui().setTextVers("\r\n"  + ">\r\n" + altText + "\r\n" );

                    }

I would like to ignore the alt tags which are blank and just print out the tags that contain text. I am using an if statement to test for empty alt tags but it still prints out all alt tags even though some contain text. Could someone help please?

Thanks

도움이 되었습니까?

해결책

Maybe change your if statement to be:

if ((altText != null) && !altText.isEmpty())
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top