一个使用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的例子:
package com.lph;
//writeDoc(parser.getDocument().getDocumentElement()); } private String indent = " "; // 缩进的标志 private String lineSeparator = "\n"; // 换行的标志 // print element with attributes NodeList children = node.getChildNodes(); break; // handle entity reference nodes // print cdata sections // print text // print processing instruction if (type == Node.ELEMENT_NODE) { /** case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); /** } else { Node child = node.getFirstChild();
import java.io.PrintWriter;
import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author Administrator
*
*/
public class TestHTMLDOM {
public static void main(String[] argv) throws Exception {
DOMParser parser = new DOMParser();
System.setProperty("http.proxyHost", "88.148.41.44");
System.setProperty("http.proxyPort", "51966");
parser.setFeature("http://xml.org/sax/features/namespaces", false);
parser.parse("c:/blog.htm");//也可以是如:http://itindex.net/blog
//print(parser.getDocument(), "");
printDOMTree(parser.getDocument());
PrintWriter w = new PrintWriter(System.out);
/** Prints the specified node, then prints all of its children. */
/**
* 打印xml/html文档
*/
public static void printDOMTree(Node node) {
int type = node.getNodeType();
switch (type) {
// print the document element
case Node.DOCUMENT_NODE: {
System.out.println("<?xml version=\"1.0\" ?>");
printDOMTree(((Document) node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() + "=\""
+ attr.getNodeValue() + "\"");
}
System.out.println(">");
if (children != null) {
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
}
}
case Node.ENTITY_REFERENCE_NODE: {
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
case Node.CDATA_SECTION_NODE: {
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
case Node.TEXT_NODE: {
System.out.print(node.getNodeValue());
break;
}
case Node.PROCESSING_INSTRUCTION_NODE: {
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print(" ");
System.out.print(data);
}
System.out.print("?>");
break;
}
}
System.out.println();
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print(">");
}
}
* 输出经过修整干净的html文档,fix up and clean html
*
* @param node
*/
private static void writeDoc(Node node) {
short type = node.getNodeType();
switch (type) {
String name = "<" + node.getNodeName();
NamedNodeMap attrs = node.getAttributes();
if (attrs != null) {
int length = attrs.getLength();
for (int i = 0; i < length; i++) {
Node attr = attrs.item(i);
name += " " + attr.getNodeName();
name += "=\"" + attr.getNodeValue() + "\"";
}
}
name += ">";
System.out.println(name);
if (children != null) {
int length = children.getLength();
for (int i = 0; i < length; i++)
writeDoc(children.item(i));
}
System.out.println("</" + node.getNodeName() + ">");
break;
}
case Node.TEXT_NODE: {
System.out.println(node.getNodeValue());
break;
}
}
}
* 抽取html文档里的文本Text
*
* @param node
* @param indent
*/
public static void print(Node node, String indent) {
// System.out.println(indent+node.getClass().getName());
if (node.getNodeValue() != null) {
if ("".equals(node.getNodeValue().trim())) {
System.out.print(indent);
System.out.println(node.getNodeValue());
}
}
while (child != null) {
print(child, indent + " ");
child = child.getNextSibling();
}
}
}