Solved

best way to remove html tags from a String in java

Posted on 2011-09-20
7
394 Views
Last Modified: 2012-05-12
I have a html file, which is in the form of a String. I want to strip off the html tags and extract the text to a String. I would like to do this in java. I have seen several ways of accomplishing this. I want to know the best way to do this.

TIA
0
Comment
Question by:bent27
  • 5
  • 2
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36567457
        String htString = "sdfsd <html> dsfsdfjds  <jkj>  sdfsd<sdfsdf/>sdfsdfs<fsfs> ";


        String res = htString.replaceAll("<[^>]+>","");

        System.out.println(res);

Open in new window


Output:

sdfsd  dsfsdfjds    sdfsdsdfsdfs 

Open in new window

0
 

Author Comment

by:bent27
ID: 36567471
what about something like


String sample = "&lt;head&gt; bla bla</head>";
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36567520

but between &lt; and &gt: - it is not the tags - so we want to keep "head" in between ?

        String htString = "&lt;head&gt; bla bla</head>;";


        String res = htString.replaceAll("(<[^>]+>)|(&[^;]+;)","");


  

        System.out.println(res);

Open in new window


Output:

head bla bla;

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36567582
You probably want it like that, because that' why they use &lt; , &gt;- to represent "<"  and ">" say in inequalities and distinguish them from  tags



        String htString = "&lt;head&gt; bla bla</head>;";


        String res = htString.replaceAll("<[^>]+>","").replace("&lt;","<").replace("&gt;",">");


       // String res1 = res.replaceAll("&[^;]+;","");

        System.out.println(res);

Open in new window



Output:

<head> bla bla;

Open in new window

0
 

Author Comment

by:bent27
ID: 36567606
sample input :

String htString = "&lt;head&gt; bla bla</head>;";


intended output :

bla bla

or, have you used jsoup, what is your take on it?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36567682
We can do it this way if you want, but I don't think it is what you want -
if they use &lt; &gt; in the HTML code - I think they do it becuae they want to use literal ">" and "<"
instead of the tags - th's owhy I don't think you need to remove the stuff between them - is that
real snippet
"&lt;head&gt; bla bla</head>;"; ?

Woulds browser really understand this as the openeing <head> tag ?
That's wahy I thiink this &lt; should be replace by "<"
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36567840
This is how you want it below  but think about what I posted above - check it - don't know if I'm right - but that was my understanding
why people write &lt; - othewise it is easier to type "<"

        String htString = "&lt;head&gt; bla bla</head>;";




          String res = htString.replaceAll("(<[^>]+>)|(&lt;.+?&gt;)","");



        System.out.println(res);

Open in new window

Output:

 bla bla;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 30
servlet requestdispatcher include and forward differences 1 22
reverse digits of a number using for loop 5 36
MySQL  on Tomcat 8 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now