一个使用NekoHTML解析html的例子

NekoHTML 可以解析、修整和净化html文档,可以自动关闭标记,修补一些常见的错误,也可以用NekoHTML从html文档里抽取文本。

NekoHTML is a simple HTML scanner and tag balancer that enables application programmers to parse HTML documents and access the information using standard XML interfaces. The parser can scan HTML files and "fix up" many common mistakes that human (and computer) authors make in writing HTML documents. NekoHTML adds missing parent elements; automatically closes elements with optional end tags; and can handle mismatched inline element tags.

NekoHTML is written using the Xerces Native Interface (XNI) that is the foundation of the Xerces2 implementation. This enables you to use the NekoHTML parser with existing XNI tools without modification or rewriting code.

下面是一个使用NekoHTML的例子:

  1. package com.lph;   
  2.   
  3.   
  4. import java.io.PrintWriter;   
  5. import org.cyberneko.html.parsers.DOMParser;   
  6. import org.w3c.dom.Document;   
  7. import org.w3c.dom.NamedNodeMap;   
  8. import org.w3c.dom.Node;   
  9. import org.w3c.dom.NodeList;   
  10. /**  
  11.  *  
  12.  * @author Administrator  
  13.  *  
  14.  */  
  15. public class TestHTMLDOM {   
  16.  public static void main(String[] argv) throws Exception {   
  17.   DOMParser parser = new DOMParser();   
  18.   System.setProperty("http.proxyHost""88.148.41.44");   
  19.   System.setProperty("http.proxyPort""51966");   
  20.   parser.setFeature("http://xml.org/sax/features/namespaces"false);   
  21.   parser.parse("c:/blog.htm");//也可以是如:http://itindex.net/blog   
  22.      
  23.   
  24.   //writeDoc(parser.getDocument().getDocumentElement());   
  25.   //print(parser.getDocument(), "");   
  26.   printDOMTree(parser.getDocument());   
  27.   PrintWriter w = new PrintWriter(System.out);   
  28.   
  29.  }   
  30.   
  31.  private String indent = " "// 缩进的标志   
  32.   
  33.  private String lineSeparator = "\n"// 换行的标志   
  34.   
  35.   
  36.  /** Prints the specified node, then prints all of its children. */  
  37.  /**  
  38.   * 打印xml/html文档  
  39.   */  
  40.  public static void printDOMTree(Node node) {   
  41.   int type = node.getNodeType();   
  42.   switch (type) {   
  43.   // print the document element   
  44.   case Node.DOCUMENT_NODE: {   
  45.    System.out.println("<?xml version=\"1.0\" ?>");   
  46.    printDOMTree(((Document) node).getDocumentElement());   
  47.    break;   
  48.   }   
  49.   
  50.    // print element with attributes   
  51.   case Node.ELEMENT_NODE: {   
  52.    System.out.print("<");   
  53.    System.out.print(node.getNodeName());   
  54.    NamedNodeMap attrs = node.getAttributes();   
  55.    for (int i = 0; i < attrs.getLength(); i++) {   
  56.     Node attr = attrs.item(i);   
  57.     System.out.print(" " + attr.getNodeName() + "=\""  
  58.       + attr.getNodeValue() + "\"");   
  59.    }   
  60.    System.out.println(">");   
  61.   
  62.    NodeList children = node.getChildNodes();   
  63.    if (children != null) {   
  64.     int len = children.getLength();   
  65.     for (int i = 0; i < len; i++)   
  66.      printDOMTree(children.item(i));   
  67.    }   
  68.   
  69.    break;   
  70.   }   
  71.   
  72.    // handle entity reference nodes   
  73.   case Node.ENTITY_REFERENCE_NODE: {   
  74.    System.out.print("&");   
  75.    System.out.print(node.getNodeName());   
  76.    System.out.print(";");   
  77.    break;   
  78.   }   
  79.   
  80.    // print cdata sections   
  81.   case Node.CDATA_SECTION_NODE: {   
  82.    System.out.print("<![CDATA[");   
  83.    System.out.print(node.getNodeValue());   
  84.    System.out.print("]]>");   
  85.    break;   
  86.   }   
  87.   
  88.    // print text   
  89.   case Node.TEXT_NODE: {   
  90.    System.out.print(node.getNodeValue());   
  91.    break;   
  92.   }   
  93.   
  94.    // print processing instruction   
  95.   case Node.PROCESSING_INSTRUCTION_NODE: {   
  96.    System.out.print("<?");   
  97.    System.out.print(node.getNodeName());   
  98.    String data = node.getNodeValue();   
  99.    {   
  100.     System.out.print(" ");   
  101.     System.out.print(data);   
  102.    }   
  103.    System.out.print("?>");   
  104.    break;   
  105.   }   
  106.   }   
  107.   
  108.   if (type == Node.ELEMENT_NODE) {   
  109.    System.out.println();   
  110.    System.out.print("</");   
  111.    System.out.print(node.getNodeName());   
  112.    System.out.print(">");   
  113.   }   
  114.  }   
  115.   
  116.  /**  
  117.   * 输出经过修整干净的html文档,fix up and clean html  
  118.   *  
  119.   * @param node  
  120.   */  
  121.  private static void writeDoc(Node node) {   
  122.   short type = node.getNodeType();   
  123.   switch (type) {   
  124.   
  125.   case Node.ELEMENT_NODE: {   
  126.    String name = "<" + node.getNodeName();   
  127.    NamedNodeMap attrs = node.getAttributes();   
  128.    if (attrs != null) {   
  129.     int length = attrs.getLength();   
  130.     for (int i = 0; i < length; i++) {   
  131.      Node attr = attrs.item(i);   
  132.      name += " " + attr.getNodeName();   
  133.      name += "=\"" + attr.getNodeValue() + "\"";   
  134.     }   
  135.    }   
  136.    name += ">";   
  137.    System.out.println(name);   
  138.   
  139.    NodeList children = node.getChildNodes();   
  140.    if (children != null) {   
  141.     int length = children.getLength();   
  142.     for (int i = 0; i < length; i++)   
  143.      writeDoc(children.item(i));   
  144.    }   
  145.    System.out.println("</" + node.getNodeName() + ">");   
  146.    break;   
  147.   }   
  148.   case Node.TEXT_NODE: {   
  149.    System.out.println(node.getNodeValue());   
  150.    break;   
  151.   }   
  152.   }   
  153.  }   
  154.   
  155.  /**  
  156.   * 抽取html文档里的文本Text  
  157.   *  
  158.   * @param node  
  159.   * @param indent  
  160.   */  
  161.  public static void print(Node node, String indent) {   
  162.   // System.out.println(indent+node.getClass().getName());   
  163.   if (node.getNodeValue() != null) {   
  164.    if ("".equals(node.getNodeValue().trim())) {   
  165.   
  166.    } else {   
  167.     System.out.print(indent);   
  168.     System.out.println(node.getNodeValue());   
  169.    }   
  170.   }   
  171.   
  172.   Node child = node.getFirstChild();   
  173.   while (child != null) {   
  174.    print(child, indent + " ");   
  175.    child = child.getNextSibling();   
  176.   }   
  177.  }   
  178. }   
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓