用于处理YouTube标签< iframe>的TagHandler? [英] TagHandler to handle Youtube tag <iframe>?

查看:61
本文介绍了用于处理YouTube标签< iframe>的TagHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Html.fromHtml()解析html.我的问题是我的html文本有youtube嵌入的链接(基本上是<iframe>标签)

I'm parsing html using Html.fromHtml(). My problem is that my html text has youtube embeded links (basically <iframe> tags)

因此,由于 HTML 类不支持<iframe>标记,我需要定义自己的 TagHandler 进行处理.我想做的是将<iframe>转换为常规的<a>标签,以便可以正确呈现它.

So, since Html class does NOT support <iframe> tag, I need to define my own TagHandler to handle it. What I'm trying to do is to convert the <iframe> to a regular <a> tag so that it can be rendered correctly.

//convert this
<iframe src="http://www.youtube.com/embed/xAEdMI2ZE88" frameborder="0" width="560" height="315"></iframe>
//To this
<a href="http://www.youtube.com/embed/xAEdMI2ZE88">Click to Watch</a>

我的问题是我找不到从<iframe>标签获取youtube src链接的方法.

My problem is that I couldn't find a way to get the src link of the youtube from the <iframe> tag.

这是我的TagHandler的handleTag()方法:

Here is my TagHandler's handleTag() method:

@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
    if (tag.equals("iframe")) {
        if(opening) {
            output.append("<a href=");
            //How to get YouTube video link and append it?
        }
        else {
            output.append("Click To Watch</a>");
        }
    }
}

谢谢.

推荐答案

我暂时接受了CommonsWare的建议,并修改了字符串before并将其传递给Html.fromHtml.

I, for now, took CommonsWare advice and modify the String before passing it to Html.fromHtml.

//Opening tag
Pattern p = Pattern.compile("<iframe src");
Matcher m = p.matcher(htmlString);
while (m.find())
    htmlString= m.replaceAll("<a href");

//Closing tag    
p = Pattern.compile("frameborder=.*</iframe>");
m = p.matcher(htmlString);
while (m.find())
    htmlString= m.replaceAll(">CLICK TO WATCH</a>");

这篇关于用于处理YouTube标签&lt; iframe&gt;的TagHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆