Escape html anchor tags with

How to escape each html dynamic anchor tag if URLs are different?

<a href="*_url">*_url</a>

I need the above code to be saved in the database as

&lt;a href="*_url"&gt;*_url&lt;/a&gt;

I've escaped other tags like <code>,<html>,<strong>... using

`StringUtils.replaceEach(post, search, replacement);` 

Because they are static tags. But the anchor tag holds "href" as dynamic content.

How can I get this done by a Javamethod at once.

Thanks!

Answers


Use StringEscapeUtils.escapeHtml() from Apache Commons Lang.


You can use java.net.URLEncoder.encode on conjunction with your StringUtils.replaceEach. You may want to parse the anchor tag and encode the same using URLEncoder .


using an XML/HTML parser and getting the text within the two tags is easy.

i got it here


David Grant's solution seems correct to me. In the meantime escapeHtml has gotten obsolete. You can choose between escapeHtml3 and escapeHtml4.

1) Add to Your Gradle Dependency List:

compile 'org.apache.commons:commons-lang3:3.3.2'

For Maven it is:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>

2)

import org.apache.commons.lang3.StringEscapeUtils;

3) Then invoke...

StringEscapeUtils.escapeHtml4(...)

Need Your Help

Cancelling a BackgroundWorker

c# .net-4.5 backgroundworker

My DoWork for backgroundworker1 sets a wait timer via a class WakeUp, it works well.

Extract continous siblings in XSL

html xslt siblings

I have an HTML file like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.