Solved

best way to remove html tags from a String in java

Posted on 2011-09-20
7
410 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
sql import cannot be resolved jsp 3 47
Java Restore security prompts not working 10 57
java example issue 3 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

861 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