一个新的HTML标签添加到Android中的HTML字符串 [英] Add a new html tag to an html string in android

查看:412
本文介绍了一个新的HTML标签添加到Android中的HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText得到的字符串。该字符串包含HTML标记。

I have a string obtained from an EditText. The string contains html tags.

Spannable s  = mainEditText.getText();
String webText = Html.toHtml(s); 

字符串的内容是:

The contents of the string is :

<p dir="ltr">test</p> 
<p dir="ltr"><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-0de7a730-3fa9-4a1e-9f82-d34e4f6e2d31-file" /><br /></p> 
<p dir="ltr"><a href="22.572646,88.363895"><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C22.572646,88.363895" /></a><br /> </p>

现在,我想要做的是,只要有一个IMG SRC标签,我想与中心标签precede它。

我应该怎么做才能得到下面的输出?

What should I do to get the following output?

<p dir="ltr">test</p> 
<p dir="ltr"><center><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-0de7a730-3fa9-4a1e-9f82-d34e4f6e2d31-file" /></center><br /></p> 
<p dir="ltr"><a href="22.572646,88.363895"><center><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C22.572646,88.363895" /></center></a><br /> </p> 

正则表达式可以解决这个问题还是应该以不同的方式来完成?

Can a regex solve the issue or should it be done in a different way?

能否JSOUP以任何方式帮助吗?有没有能胜任任何其他类型的HTML解析器?

Can JSOUP help in any way? Is there any other type of HTML parser which can do the job?

推荐答案

我实现了由 mourphy 建议JSOUP解决方案。但是,我已经编辑的方法一点,它做了奇迹为我。新的方法是:

I implemented the JSOUP solution suggested by mourphy. But, I had edited the method a little and it did the miracle for me. The new method is:

public String wrapImgWithCenter(String html){
     Document doc = Jsoup.parse(html);
        doc.select("img").wrap("<center></center>");
        return doc.html();
}

感谢mourphy和VKS对你有所帮助!

Thanks mourphy and vks for your help!

这篇关于一个新的HTML标签添加到Android中的HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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