DOM已经支持html解析,Xerces也已经提供了DOM3的实现。BTW:no pains, no gains

解决方案 »

  1.   

    ^_^ 有现成的,自己搜索去吧.no search ,no gains:)
      

  2.   

    import java.io.*;
    import org.w3c.tidy.Tidy;
    /**
     * HTML文件整理
     * 
     * 从Test16.java修改而来
     *
     * 运行参数 
     * java Test17 <html_file> <new_html_file> <log_file> <config_file>
     * 
     */
    public class Test17 implements Runnable {
        private String srcFileName;
        private String outFileName;
        private String errOutFileName;
        private String configFileName;    public Test17(String srcFileName, String outFileName, String errOutFileName,
            String confName) {
            this.srcFileName = srcFileName;
            this.outFileName = outFileName;
            this.errOutFileName = errOutFileName;
            this.configFileName= confName;
        }    public void run() {
            BufferedInputStream in;
            FileOutputStream out;
            Tidy tidy = new Tidy();
    tidy.setConfigurationFromFile(configFileName);
            try {
                tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true));
                in = new BufferedInputStream(new FileInputStream(srcFileName));
                out = new FileOutputStream(outFileName);
                tidy.parseDOM(in, out);
            } catch (IOException e) {
                System.out.println(this.toString() + e.toString());
            }
        }    public static void main(String[] args) {
            Test17 t1 = new Test17(args[0], args[1], args[2], args[3]);
            Thread th1 = new Thread(t1);
            th1.start();
        }
    }
      

  3.   

    配置文件和错误日志可以为空! 
    下面是配置文件参考:
    //JTidy设置文件参考如下:
    // sample config file for HTML tidy
    indent: auto
    indent-spaces: 4
    wrap: 72
    up: yes
    output-xml: yes
    input-xml: no
    show-warnings: yes
    numeric-entities: yes
    quote-s: yes
    quote-nbsp: yes
    quote-ampersand: no
    break-before-br: no
    uppercase-tags: no
    uppercase-attributes: no
    char-encoding: raw
    new-inline-tags: cfif, cfelse, math, mroot, 
    mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,
    munder, mover, mmultiscripts, msup, msub, mtext,
    mprescripts, mtable, mtr, mtd, mth
    new-blocklevel-tags: cfoutput, cfquery
    new-empty-tags: cfelse
      

  4.   

    将html转换成DOM树以后,就可以用任何XML解析器进行解析了!
      

  5.   

    比如说遍历此html文档(当然是转换后的,也就是XHtml):
    Document document = tidy.parseDOM( in,out );
    travers( document );/**
     * Traverse DOM Tree
     *
     */
    public void traverse( Node cNode ) {
    String eleName = null;
    switch( cNode.getNodeType() ) {
    case Node.DOCUMENT_NODE:
    System.out.println( "Element " + cNode.getNodeName() );
    processChildren( cNode.getChildNodes() );
    break;
    case Node.ELEMENT_NODE:
    eleName = cNode.getNodeName();
    System.out.println("Element " + eleName);
    NamedNodeMap attributeMap = cNode.getAttributes();
    int numAttrs = attributeMap.getLength();
    for(int i = 0; i < attributeMap.getLength(); i++ ) {
    Attr attribute = (Attr)attributeMap.item(i);
    String attrName = attribute.getNodeName();
    String attrValue = attribute.getNodeValue();
    System.out.println( attrName + " = " + attrValue );
    }
    processChildren(cNode.getChildNodes());
    break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
    System.out.println( "Text " + cNode.getNodeValue() );
    if( !cNode.getNodeValue().trim().equals("") ) {
    System.out.println( "eleName " + eleName );
    System.out.println( "Text " + cNode.getNodeValue() );
    }
    break;
    }
    }private void processChildren( NodeList nList ){
    if( nList.getLength()!=0 ){
    for( int i = 0; i < nList.getLength(); i++ ) {
    traverse( nList.item(i) );
    }
    }
    }
      

  6.   

    标准的JDK里面就肯定有,理由:javax.swing.JComponent本身就能够显示(相对简单的,符合w3c规范的)HTML,具体是哪个class作为Parser不太清楚,但肯定在jdk内
      

  7.   

    see javax.swing.text.html.parser.DocumentParser
      

  8.   

    上面javax.swing.JComponent说错了,是JEditorPane