TXT content _jsp programming to get rid of tags

Source: Internet
Author: User
Tags stringbuffer
Copy Code code as follows:

NodeList Body_nodes=this.getparser (). Parse (Body_filter);
for (int i=0;i<body_nodes.size (); i++)
{
Node Node=body_nodes.elementat (i);

Parser body_parser=new Parser (node.tohtml ());
Textextractingvisitor visitor=new textextractingvisitor ();
Body_parser.visitallnodeswith (visitor);
Body.append (Visitor.getextractedtext ());
}

Textextractingvisitor,visitallnodeswith and other classes and methods are visitor important but also rare.
The following source code is attached:
Copy Code code as follows:

Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.util.Date;

Import Org.htmlparser.Node;
Import Org.htmlparser.NodeFilter;
Import Org.htmlparser.Parser;
Import Org.htmlparser.filters.AndFilter;
Import Org.htmlparser.filters.HasAttributeFilter;
Import Org.htmlparser.filters.HasChildFilter;
Import Org.htmlparser.filters.TagNameFilter;
Import org.htmlparser.util.NodeList;
Import Org.htmlparser.visitors.TextExtractingVisitor;

Import Com.extractor.Extractor;

public class Extractorhangdian extends extractor{
public void Extract ()
{
BufferedWriter Bw=null;
String Indextime;
String title;
StringBuffer body=new StringBuffer ();;
Nodefilter time_filter=new Andfilter (New Tagnamefilter ("Font"), New Hasattributefilter ("Color", "#808080"));
Nodefilter title_filter1=new Andfilter (New Tagnamefilter ("TD"), new Haschildfilter (New Tagnamefilter ("B"));
Nodefilter body_filter=new Andfilter (New Tagnamefilter ("TD"), new Haschildfilter (New Tagnamefilter ("P"));

Try
{
NodeList Title_nodes=this.getparser (). Parse (Title_filter1);
Node node=title_nodes.elementat (0);
NodeList Node2=node.getchildren ();
Title=node2.elementat (0). toHtml (); /* ' \ r \ n ' *
Title=node2.elementat (1). toHtml (); /*font color= "#000080" style= "font-size:14.4px*/
Title=node2.elementat (2). toHtml (); /* b * *
Title=node2.elementat (3). toHtml (); * * Textbook subscription and teacher's book registration notice * *


Bw=new BufferedWriter (New FileWriter (This.getoutputpath () +title+ ". txt"));

String Url_seg1=getinputfilepath (). substring (3,30);
int End=getinputfilepath (). LastIndexOf (".");
String Url_seg2=getinputfilepath (). substring (end);
String url_seg=url_seg1+ ". asp?" +URL_SEG2;
Url_seg=url_seg.replaceall ("\\\\", "/");
String url= "http://" +url_seg;

Bw.write (Url+newline);
Bw.write (Title+newline);


}
catch (Exception e)
{
E.printstacktrace ();
}

This.getparser (). reset ();
Try
{
NodeList Time_nodes=this.getparser (). Parse (Time_filter);
Node Time_node=time_nodes.elementat (1);//Here "1" represents the second element that conforms to Time_filter
Indextime=time_node.getnextsibling (). toHtml ();

Bw.write (Indextime+newline);
}
catch (Exception e)
{
E.printstacktrace ();
}

This.getparser (). Reset ()//get rid of all txt text of the label
Try
{
NodeList Body_nodes=this.getparser (). Parse (Body_filter);
for (int i=0;i<body_nodes.size (); i++)
{
Node Node=body_nodes.elementat (i);

Parser body_parser=new Parser (node.tohtml ());
Textextractingvisitor visitor=new textextractingvisitor ();
Body_parser.visitallnodeswith (visitor);
Body.append (Visitor.getextractedtext ());
}
Bw.write (Body+newline);

}
catch (Exception e)
{
E.printstacktrace ();
}

Try
{
if (bw!=null)
Bw.close ();
}catch (IOException e)
{
E.printstacktrace ();
}
}
}

Here by the way, when bw did not turn off, how to read not to go in, did me for several days, depressed dead, think up on the fire, pay attention!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.