Arvind
Arvind

Reputation: 6474

How to find a specific meta tag

I am trying to retrieve a meta tag (Tag name=Generator) using Jsoup parser in java.

The code I have is given below:

Elements metalinks=doc.select("meta"); // meta

boolean metafound=false;
for (Element singlemeta : metalinks) 
{
    metatagname = singlemeta.attr("abs:name");
    metatagcontent = singlemeta.attr("abs:content");
    if((metatagname=="Generator")||(metatagname=="generator")||(metatagname=="GENERATOR")){
        //  this is the tag we want to get value of...
        metarequired=metatagcontent;
        metafound=true;
    }
}   
if(metafound==false)
    metarequired="NOT_FOUND";

However I am unable to extract the meta GENERATOR tag correctly.

One example of this tag is now given below:

<meta name="generator" content="Test page" />

For the very first line in code given above, I also tried the following code but that also does not work:

//Elements metalinks= doc.getElementsByTag("meta");

How do I extract the meta tag correctly?

Upvotes: 4

Views: 10982

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

It almost looks as if you're making it too complicated. What if you started out with something simple like this:

Elements metalinks = doc.select("meta[name=generator]");

Upvotes: 15

Related Questions