0
<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.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • 3
    It's not very clear what you're asking here. Can you give further detail on what you mean by *"I want to replace the bold tag from the entire source code"*? (Click the "edit" link under your post to update it with more information.) – Rob Hruska Nov 21 '11 at 18:50

4 Answers4

1

If I understand correctly, you want to remove any <b> tags from your HTML? If so, you can use jQuery:

$('b').contents().unwrap();

See: Remove a HTML tag but keep the innerHtml

Community
  • 1
  • 1
HellaMad
  • 5,294
  • 6
  • 31
  • 53
  • I could be wrong, but I'm not sure jQuery's involved here? The OP tagged it [Java] (although it could very well be incorrectly tagged). – Rob Hruska Nov 21 '11 at 18:51
  • True. I guess I assumed too Javascript was what the OP wanted before thinking about it. – HellaMad Nov 21 '11 at 18:52
  • 1
    Don't worry, though. [There's always room for a jQuery answer!](http://meta.stackexchange.com/questions/19478/the-many-memes-of-meta/19492#19492) – Rob Hruska Nov 21 '11 at 18:56
1

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.

shift66
  • 11,760
  • 13
  • 50
  • 83
  • 1
    This assumes that he means that he wants to remove the bolding tags but not the enclosed content -- "0insen" in the example. 'Not saying that isn't what he means, just pointing out the ambiguity in the question. – Jay Nov 21 '11 at 19:17
1

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.

laz
  • 28,320
  • 5
  • 53
  • 50
0

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.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • (This assumes you're doing this in Java, and not JavaScript - as another answer is assuming...) – ziesemer Nov 21 '11 at 18:51
  • ziesemer - The question is tagged as being Java by the original author, so I don't think a JavaScript solution is required. – laz Nov 21 '11 at 19:16