Home ยป How to replace a tag with a space in a text file?

How to replace a tag with a space in a text file?

<ul class=”uinfo2″><li class=”uname2″>ruo</li>
<ul class=”uinfo2″><li class=”uname”><b>Oinsen</b></li>

$(‘b’).contents().unwrap();

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();

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)));
    }
}

Add Comment

Click here to post a comment

Your email address will not be published. Required fields are marked *