帐前卒专栏

Without software, we are nothing.

htmlParser Rewrite HTML

Yesteday, I have tried many methods to rewrite HTML. And find TagNode can not write TextNode, so if I want to replace “<p> id=”hello”></p>” to “<p id=”hello”>it’s me</p>”. There is no method to support. And in htmlParser, there is a method call setText(). But setText is only set text into<p id=”hello”>, like “<p id=”hello” class=”pp”>”. The method can not write text into”<p></p>”. If you want to add a TextNode as TagNode child. HtmlParser can not support this idea also. Like:

NodeList nl = p.parse(null); NodeVisitor nv = new NodeVisitor(){

public void visitTag(Tag tag) {
    if (tag instanceof ParagraphTag) {
        String attribute = tag.getAttribute("id");
        if (attribute == null){
            return;
        }
        TextNode tn = new TextNode(tag.getText());
        tn.setText("sidafnaf ");
        if(tag.getAttribute("id").equals("hello")){
            Text t = new TextNode("abc");
            t.setParent(tag);
            // It does not work
            NodeList temp = new NodeList();
            temp.add(t);
            tag.setChildren(temp);
        } else if (tag.getAttribute("id").equals("author")){
            tag.setAttribute("innerHTML","id a");
        }
    } else if (tag instanceof LinkTag){
        if (tag.getAttribute("id").equals("come-from")){
            // can write an attribute innerHtml = "ok?"
            tag.setAttribute("innerHtml","ok?");
            // can rewrite URL attribute 
            tag.setAttribute("href", "http://www.google.com");
        }
    }
}

};

Solve this problem, you should flag where you want to insert. like<p>messgae</p> Then use following code:

NodeList nl = p.parse(null); NodeVisitor nv = new NodeVisitor(){ public void visitStringNode(Text string){

      if(string.getText().equals("message")){
           string.setText("It's me");
      }
      // if you want change attributes of &lt;p&gt;    
      TagNode tn = string.getParent();
      tn.setAttribute("id","good!");

} }

Now it’s done.

Comments