<ul class="uinfo2"><li class="uname2">ruo</li>
<ul class="uinfo2"><li class="uname"><b>Oinsen</b></li>
If the file contains the above HTML lines, I want to replace the bold tag from the entire source code.
<ul class="uinfo2"><li class="uname2">ruo</li>
<ul class="uinfo2"><li class="uname"><b>Oinsen</b></li>
If the file contains the above HTML lines, I want to replace the bold tag from the entire source code.
If I understand correctly, you want to remove any <b>
tags from your HTML? If so, you can use jQuery:
$('b').contents().unwrap();
read all content of file and store in a String (for example String content;) using replaceAll method.
line.replaceAll("<b>", " ").replaceAll("</b>"," ");
And finally write new String into that file.
File file = new File("input.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line = null;
while((line=reader.readLine()) != null)
sb.append(line.replaceAll("<b>", " ").replaceAll("</b>", " "));
FileWriter writer = new FileWriter(file);
writer.write(sb.toString());
writer.flush();
writer.close();
here is the code.
Using jsoup can accomplish this:
import java.io.IOException;
import nu.xom.ParsingException;
import nu.xom.ValidityException;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.xml.sax.SAXException;
public class HtmlTest {
public static void main(final String[] args) throws SAXException, ValidityException, ParsingException, IOException {
String[] tagsToKeep = new String[] {"ul", "li"};
String html = "<ul class=\"uinfo2\"><li class=\"uname2\">ruo</li>\n<ul class=\"uinfo2\"><li class=\"uname\"><b>Oinsen</b></li>";
System.out.println(JSoup.clean(html, Whitelist.none().addTags(tagsToKeep)));
}
}
Initialize tagsToKeep
with all of the tags you do not want removed and pass it to addTags
.
You could use Regular Expressions, but they quickly become complicated when trying to match HTML. If your file in XHTML compliant (which this appears to be), I'd recommend loading the file as XML and using XPath to determine if this meets your criteria - and then DOM operations to replace the bold tag however you require.