lis
lis

Reputation: 680

How to extract meta tags from website on android?

is there a smart way to read the content of metatags from an URL in android? I'll show a webpage in the webview on android and want to read some informations from the metatag inside. Is the only way to parse the string of the webpage to find the special string "meta name="x-..." content="!!!" or is there any smarter way??

Upvotes: 2

Views: 6251

Answers (1)

Jorgesys
Jorgesys

Reputation: 126523

A smart way would be using Jericho Library

supposing you have an html file like this

<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<link href="styleUrgente.css" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width = 320, initial-scale = 1.0, user-scalable = no"/>
<meta name="joc-height" value="120"/>
<meta name="joc-enabled" value="1"/>
</head>
<body margin="0" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<script src="chrome-extension://bmagokdooijbeehmkpknfglimnifench/googleChrome.js"/>
</html>

for example to get the value of meta tag with name "joc-height" you can use this method:

public String extractAllText(String htmlText){
        Source source = new Source(htmlText);   
        String strData = "";        
        List<Element> elements = source.getAllElements("meta");

        for(Element element : elements )
        {
            final String id = element.getAttributeValue("name"); // Get Attribute 'id'
             if( id != null && id.equals("joc-height")){
                 strData = element.getAttributeValue("value").toString();    
                   }
        }
        return strData;
    }

and you will get the value of "120"

Upvotes: 2

Related Questions