这个程序的目的是想遍历HTML。如果得到button控件我就。做一些处理(如:删除 onclick事件)
                  如果得到span控件我就做。一些处理(如:删除 onkeydown、onmouseover事件)
                 如果得到style控件我就做。一些处理(如:font-size超过200我就变为100)
         如果得到img控件我就做。一些处理(如:....)
               ..........................................
                      .......................................... 
然后把处理过的html打印出来现在遇到的问题是:
1.我遍历属性的时候,由于选择了一个,不是很规则的Html.String,所以第一行的style被当作一个属性遍历出来了。
而font-size:200被当作这个属性的Value遍历出来了!即:
Name==style
Value==font-size: 200
而不能把font-size当做属性遍历出来,而200当作font-size的数值遍历出来的!要怎么才能达到这个效果呢?
但是后面font写法的,face ,color ,size都会当成属性遍历出来!即:
Name==face
Value==楷体
2.经过处理后!我toHtml以后得到的字符串比以前膨胀了很多,前面和后面的都重复了不知道是为什么阿?
<span onkeydown=""style="font-size:100">span1<p>
<span onkeydown="" style="font-size: 200">span2</span></p>
<button onclick="alert('button')">button1</button><p>
<font face="楷体" color="red" size="300">font1</font></p><p></p>
</span><p><span onkeydown="" style="font-size: 200">span2</span></p>
<span onkeydown="" style="font-size: 200">span2</span>
<button onclick="alert('button')"><p><font face="楷体" color="red" size="300">font1</font></p>
<font face="楷体" color="red" size="300"><p></p>
import java.util.*;
import org.htmlparser.*;
import org.htmlparser.util.*;
import org.htmlparser.visitors.*;
public class HtmPar {
public static void main(String[] args) throws Exception {
StringBuffer sbHtml = new StringBuffer();
sbHtml
.append(
"<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1")
.append(
"<p><span onkeydown=\"alert('onkeydown')\" style=\"font-size: 200\">span2</span></p>")
.append("<button onclick=\"alert('button')\">button1</button>")
.append(
"<p><font face=\"楷体\" color=\"red\" size=\"300\">font1</font><p>"); System.out.println(new HtmPar().filterBody(sbHtml.toString())); } public String filterBody(String body) throws Exception {
String reValue = "";
Parser parser = Parser.createParser(body.toString(), "GBK");
AttribVisitor av = new AttribVisitor();
parser.visitAllNodesWith(av);
reValue = av.getHtml();
return reValue;
}
}// 遍历
final class AttribVisitor extends NodeVisitor {
private NodeList tables = new NodeList();
public void visitTag(Tag tag) {
Vector<Attribute> _vr = tag.getAttributesEx();
int vrL = _vr.size();
Attribute _arr = null;
String arrName = null;
for (int i = 0; i < vrL; i++) {
_arr = _vr.get(i);
arrName = _arr.getName();
System.out.println("***************************");
System.out.println("Name==" + arrName);
System.out.println("Value==" + _arr.getValue());
System.out.println("***************************");
if (arrName != null && arrName.equals("onkeydown"))
_arr.setValue("");
_vr.set(i, _arr);
}
tag.setAttributesEx(_vr);
tables.add(tag);
}
public synchronized String getHtml() {
String reValue = null;
reValue = tables.toHtml();
return reValue;
}
}原是字符串如下:
<span onkeydown="alert('onkeydown')"style="font-size:100">span1
<p><span onkeydown="alert('onkeydown')" style="font-size: 200">span2</span></p>
<button onclick="alert('button')">button1</button>
<p><font face="楷体" color="red" size="300">font1</font><p>
根据上面需求希望得到结果:
<span style="font-size:100">span1                               //去掉onkeydown事件
<p><span style="font-size: 200">span2</span></p> //去掉onkeydown事件
<button>button1</button> //去掉onclick事件
<p><font face="楷体" color="red" size="100">font1</font><p> //size超过200变成100

解决方案 »

  1.   

    这个程序的目的是想遍历HTML。如果得到button控件我就。做一些处理(如:删除 onclick事件)
                      如果得到span控件我就做。一些处理(如:删除 onkeydown、onmouseover事件)
                     如果得到style控件我就做。一些处理(如:font-size超过200我就变为100)
             如果得到img控件我就做。一些处理(如:....)
                   ..........................................
                          .......................................... 
    然后把处理过的html打印出来现在遇到的问题是:
    1.我遍历属性的时候,由于选择了一个,不是很规则的Html.String,所以第一行的style被当作一个属性遍历出来了。
    而font-size:200被当作这个属性的Value遍历出来了!即:
    Name==style
    Value==font-size: 200
    而不能把font-size当做属性遍历出来,而200当作font-size的数值遍历出来的!要怎么才能达到这个效果呢?
    但是后面font写法的,face ,color ,size都会当成属性遍历出来!即:
    Name==face
    Value==楷体
    2.经过处理后!我toHtml以后得到的字符串比以前膨胀了很多,前面和后面的都重复了不知道是为什么阿?
    <span onkeydown=""style="font-size:100">span1<p>
    <span onkeydown="" style="font-size: 200">span2</span></p>
    <button onclick="alert('button')">button1</button><p>
    <font face="楷体" color="red" size="300">font1</font></p><p></p>
    </span><p><span onkeydown="" style="font-size: 200">span2</span></p>
    <span onkeydown="" style="font-size: 200">span2</span>
    <button onclick="alert('button')"><p><font face="楷体" color="red" size="300">font1</font></p>
    <font face="楷体" color="red" size="300"><p></p>
    import java.util.*;
    import org.htmlparser.*;
    import org.htmlparser.util.*;
    import org.htmlparser.visitors.*;
    public class HtmPar {
    public static void main(String[] args) throws Exception {
    StringBuffer sbHtml = new StringBuffer();
    sbHtml
    .append(
    "<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1")
    .append(
    "<p><span onkeydown=\"alert('onkeydown')\" style=\"font-size: 200\">span2</span></p>")
    .append("<button onclick=\"alert('button')\">button1</button>")
    .append(
    "<p><font face=\"楷体\" color=\"red\" size=\"300\">font1</font><p>"); System.out.println(new HtmPar().filterBody(sbHtml.toString())); } public String filterBody(String body) throws Exception {
    String reValue = "";
    Parser parser = Parser.createParser(body.toString(), "GBK");
    AttribVisitor av = new AttribVisitor();
    parser.visitAllNodesWith(av);
    reValue = av.getHtml();
    return reValue;
    }
    }// 遍历
    final class AttribVisitor extends NodeVisitor {
    private NodeList tables = new NodeList();
    public void visitTag(Tag tag) {
    Vector<Attribute> _vr = tag.getAttributesEx();
    int vrL = _vr.size();
    Attribute _arr = null;
    String arrName = null;
    for (int i = 0; i < vrL; i++) {
    _arr = _vr.get(i);
    arrName = _arr.getName();
    System.out.println("***************************");
    System.out.println("Name==" + arrName);
    System.out.println("Value==" + _arr.getValue());
    System.out.println("***************************");
    if (arrName != null && arrName.equals("onkeydown"))
    _arr.setValue("");
    _vr.set(i, _arr);
    }
    tag.setAttributesEx(_vr);
    tables.add(tag);
    }
    public synchronized String getHtml() {
    String reValue = null;
    reValue = tables.toHtml();
    return reValue;
    }
    }原是字符串如下:
    <span onkeydown="alert('onkeydown')"style="font-size:100">span1
    <p><span onkeydown="alert('onkeydown')" style="font-size: 200">span2</span></p>
    <button onclick="alert('button')">button1</button>
    <p><font face="楷体" color="red" size="300">font1</font><p>
    根据上面需求希望得到结果:
    <span style="font-size:100">span1                               //去掉onkeydown事件
    <p><span style="font-size: 200">span2</span></p> //去掉onkeydown事件
    <button>button1</button> //去掉onclick事件
    <p><font face="楷体" color="red" size="100">font1</font><p> //size超过200变成100
      

  2.   

    这个程序的目的是想遍历HTML。如果得到button控件我就。做一些处理(如:删除 onclick事件)
                      如果得到span控件我就做。一些处理(如:删除 onkeydown、onmouseover事件)
                     如果得到style控件我就做。一些处理(如:font-size超过200我就变为100)
             如果得到img控件我就做。一些处理(如:....)
                   ..........................................
                          .......................................... 
    然后把处理过的html打印出来现在遇到的问题是:
    1.我遍历属性的时候,由于选择了一个,不是很规则的Html.String,所以第一行的style被当作一个属性遍历出来了。
    而font-size:200被当作这个属性的Value遍历出来了!即:
    Name==style
    Value==font-size: 200
    而不能把font-size当做属性遍历出来,而200当作font-size的数值遍历出来的!要怎么才能达到这个效果呢?
    但是后面font写法的,face ,color ,size都会当成属性遍历出来!即:
    Name==face
    Value==楷体
    2.经过处理后!我toHtml以后得到的字符串比以前膨胀了很多,前面和后面的都重复了不知道是为什么阿?
    <span onkeydown=""style="font-size:100">span1<p>
    <span onkeydown="" style="font-size: 200">span2</span></p>
    <button onclick="alert('button')">button1</button><p>
    <font face="楷体" color="red" size="300">font1</font></p><p></p>
    </span><p><span onkeydown="" style="font-size: 200">span2</span></p>
    <span onkeydown="" style="font-size: 200">span2</span>
    <button onclick="alert('button')"><p><font face="楷体" color="red" size="300">font1</font></p>
    <font face="楷体" color="red" size="300"><p></p>
    import java.util.*;
    import org.htmlparser.*;
    import org.htmlparser.util.*;
    import org.htmlparser.visitors.*;
    public class HtmPar {
    public static void main(String[] args) throws Exception {
    StringBuffer sbHtml = new StringBuffer();
    sbHtml
    .append(
    "<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1")
    .append(
    "<p><span onkeydown=\"alert('onkeydown')\" style=\"font-size: 200\">span2</span></p>")
    .append("<button onclick=\"alert('button')\">button1</button>")
    .append(
    "<p><font face=\"楷体\" color=\"red\" size=\"300\">font1</font><p>"); System.out.println(new HtmPar().filterBody(sbHtml.toString())); } public String filterBody(String body) throws Exception {
    String reValue = "";
    Parser parser = Parser.createParser(body.toString(), "GBK");
    AttribVisitor av = new AttribVisitor();
    parser.visitAllNodesWith(av);
    reValue = av.getHtml();
    return reValue;
    }
    }// 遍历
    final class AttribVisitor extends NodeVisitor {
    private NodeList tables = new NodeList();
    public void visitTag(Tag tag) {
    Vector<Attribute> _vr = tag.getAttributesEx();
    int vrL = _vr.size();
    Attribute _arr = null;
    String arrName = null;
    for (int i = 0; i < vrL; i++) {
    _arr = _vr.get(i);
    arrName = _arr.getName();
    System.out.println("***************************");
    System.out.println("Name==" + arrName);
    System.out.println("Value==" + _arr.getValue());
    System.out.println("***************************");
    if (arrName != null && arrName.equals("onkeydown"))
    _arr.setValue("");
    _vr.set(i, _arr);
    }
    tag.setAttributesEx(_vr);
    tables.add(tag);
    }
    public synchronized String getHtml() {
    String reValue = null;
    reValue = tables.toHtml();
    return reValue;
    }
    }原是字符串如下:
    <span onkeydown="alert('onkeydown')"style="font-size:100">span1
    <p><span onkeydown="alert('onkeydown')" style="font-size: 200">span2</span></p>
    <button onclick="alert('button')">button1</button>
    <p><font face="楷体" color="red" size="300">font1</font><p>
    根据上面需求希望得到结果:
    <span style="font-size:100">span1                               //去掉onkeydown事件
    <p><span style="font-size: 200">span2</span></p> //去掉onkeydown事件
    <button>button1</button> //去掉onclick事件
    <p><font face="楷体" color="red" size="100">font1</font><p> //size超过200变成100
      

  3.   

    上面不太清楚啊!看下面加了代码 script的啊
      

  4.   

    lz建议你发代码的时候最好用代码的格式发,那样还好看一下。
    帮lz顶了
      

  5.   

    .append("<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1")
      好像是这里少了一个结束符号!
     
    .append("<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1</span>")
      

  6.   

    你这种方式的没有用过
    不过如果只是要读入一个HTML然后进行处理的话
    完全可以用正则表达式来搞
      

  7.   

    用HasAttributeFilter,TagNameFilter,AndFilter搞定
      

  8.   


    String spanStr = "<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1 ";
    String buttonStr = "<button onclick=\"alert('button')\">button1 </button>";
    String fontSizeStr = "<p> <font face=\"楷体\" color=\"red\" size=\"2006\">font1 </font> <p>";

    String spanReg = "(<span )onkeydown=\"alert\\('onkeydown'\\)\"";
    String buttonReg = "(<button) onclick=\"alert\\('button'\\)\"";
    String fontSizeReg = "(<p> <font face=\"楷体\" color=\"red\" size=\")2[0-9][0-9]*";

    spanStr = spanStr.replaceAll(spanReg, "$1");
    buttonStr = buttonStr.replaceAll(buttonReg, "$1");
    fontSizeStr = fontSizeStr.replaceAll(fontSizeReg, "$1100");

    System.out.println("spanStr =" +spanStr);
    System.out.println("buttonStr =" +buttonStr);
    System.out.println("fontSizeStr =" +fontSizeStr);
      

  9.   

    /**
     * 
     */
    package houlei.test.htmlParser;import java.util.Vector;import org.htmlparser.Attribute;
    import org.htmlparser.Node;
    import org.htmlparser.Parser;
    import org.htmlparser.Tag;
    import org.htmlparser.util.NodeList;
    import org.htmlparser.util.ParserException;/**
     * 该类创建于 2008-9-2 下午12:48:13
     * 
     * @version 1.0.0
     * @author 侯磊
     */
    public class Test { public static void main(String[] args) throws ParserException {
    StringBuffer sbHtml = new StringBuffer();
    sbHtml.append("<span onkeydown=\"alert('onkeydown')\"style=\"font-size:100\">span1");
    sbHtml.append("<p>");
    sbHtml.append("<span onkeydown=\"alert('onkeydown')\" style=\"font-size: 200\">span2</span>");
    sbHtml.append("</p>");
    sbHtml.append("<button onclick=\"alert('button')\">button1</button>");
    sbHtml.append("<p><font face=\"楷体\" color=\"red\" size=\"300\">font1</font></p>");

    System.out.println(sbHtml.toString());

    Parser parser = Parser.createParser(sbHtml.toString(), "GBK");
    NodeList list = parser.parse(null);
    visitNodeList(list);
    System.out.println(list.toHtml());
    }

    private static void visitNodeList(NodeList list){
    for(int i=0;i<list.size();i++){
    Node node = list.elementAt(i);
    if(node instanceof Tag){
    visitTag((Tag)node);
    }
    NodeList children = node.getChildren();
    if(children!=null && children.size()>0){
    visitNodeList(children);
    }
    }
    } private static void visitTag(Tag tag) {
    Vector attrs = tag.getAttributesEx();
    for(int i=0;i<attrs.size();i++){
    Object obj = attrs.elementAt(i);
    if(obj!=null && obj instanceof Attribute){
    visitAttribute((Attribute)obj,tag);
    }
    }
    } private static void visitAttribute(Attribute attribute, Tag tag) {
    String tagName = tag.getTagName();
    if(tagName.equalsIgnoreCase(Tag_Span) && Attr_OnKeyDown.equalsIgnoreCase(attribute.getName())){
    tag.removeAttribute(Attr_OnKeyDown);
    }
    if(tagName.equalsIgnoreCase(Tag_Font) && Attr_Size.equalsIgnoreCase(attribute.getName())){
    try{
    if(Integer.parseInt(attribute.getValue())>Max_Font_Size){
    attribute.setValue(String.valueOf(Default_Font_Size));
    }
    }catch(Exception e){}
    }

    }
    private static final String Attr_OnKeyDown="onkeydown";
    private static final String Tag_Span = "span";
    private static final String Attr_Size = "size";
    private static final String Tag_Font = "font";
    private static final int Max_Font_Size = 200;
    private static final int Default_Font_Size = 100;
    }
      

  10.   

    对HtmlParser不熟悉,所以,按我自己熟悉的方式来做了。
    上述代码仅供参考。
    谢谢。
      

  11.   

    所以现在提倡使用xml来规范html。
    我就特别讨厌dreamweaver加入那种不规范的标签。针对你这种情况是否可以想办法先把标签改成规范的,然后再解析?
    或者研究一下正则表达式看看。无论如何感觉都是很庞大的任务。