import org.apache.html.dom.HTMLDocumentImpl;import org.cyberneko.html.parsers.DOMFragmentParser;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.DocumentFragment;import org.w3c.dom.NodeList;import org.w3c.dom.html.HTMLDocument;import org.xml.sax.InputSource;import java.net.*;
public class NekoTextExtractor { public DocumentFragment getDocument(InputStream is){ DocumentFragment node=null; try{ DOMFragmentParser parser=new DOMFragmentParser(); node=new HTMLDocumentImpl().createDocumentFragment(); parser.parse(new InputSource(is),node); }catch(Exception ex){ ex.printStackTrace(); } return node; } public DocumentFragment getDocument(Reader reader){ DocumentFragment node=null; try{ DOMFragmentParser parser=new DOMFragmentParser(); node=new HTMLDocumentImpl().createDocumentFragment(); parser.parse(new InputSource(reader),node); }catch(Exception ex){ ex.printStackTrace(); } return node; } public void getText(StringBuffer sb,Node node)throws Exception{ if(enabledNode(node)){ if(node.getNodeType()==Node.TEXT_NODE){ String value=node.getNodeValue(); sb.append(value.trim()); } else{ NodeList nodes=node.getChildNodes(); for(int i=0;i<nodes.getLength();i++){ Node n=nodes.item(i); getText(sb,n); } } } } public String extractText(String source)throws Exception{ StringReader reader=new StringReader(source); return extractText(reader); } public String extractText(InputStream is)throws Exception{ Node node=getDocument(is); StringBuffer sb=new StringBuffer(); getText(sb,node); return sb.toString(); } public String extractText(Reader reader)throws Exception{ Node node=getDocument(reader); StringBuffer sb=new StringBuffer(); getText(sb,node); return sb.toString(); } public static void main(String[] args)throws Exception { URL url=new URL("http://news.sina.com.cn"); HttpURLConnection uc=(HttpURLConnection)url.openConnection(); StringBuffer sb=new StringBuffer(); NekoTextExtractor nte=new NekoTextExtractor(); Node node=nte.getDocument(uc.getInputStream()); nte.getText(sb,node); System.out.println(sb.toString()); } public boolean enabledNode(Node node){ if(node.getNodeName().equalsIgnoreCase("script")) return false; else if(node.getNodeName().equalsIgnoreCase("link")) return false; else if(node.getNodeName().equalsIgnoreCase("style")) return false; else if(node.getNodeType()==Node.COMMENT_NODE) return false; else return true; } }