java去标签
❶ 跪求java去除HTML标签的代码!
如果距离云南旅游指南最近的一对标签是确定的话
先找到<spanstyle 然后获取从它开始的第一个>
然后再找距离它最近的<
然后substing就解决了
❷ java 如何去除html中的一个指定标签和指定标签里的内容
java处理html指定标签最好用正则表达式。例如要去除html中所有的h1标签和类容就可以用下面的演示代码:
packagekonw.reg;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassRemoveTag
{
publicstaticvoidmain(String[]args)
{
FileReaderfr;
Stringcontent=null;
Stringregex="<[Hh]1>.*</[Hh]1>";
try
{
fr=newFileReader("tag.html");
BufferedReaderbr=newBufferedReader(fr);
Stringstr=null;
StringBuffersb=newStringBuffer();
while((str=br.readLine())!=null)
{
sb.append(str+" ");
}
content=sb.toString();
br.close();
}catch(FileNotFoundExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(content);
StringBuffersb1=newStringBuffer();
while(matcher.find())
{
sb1.append(matcher.replaceAll("")+" ");
}
try
{
FileWriterfw=newFileWriter("tag.html");
BufferedWriterbw=newBufferedWriter(fw);
fw.write(sb1.toString());
bw.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
❸ java如何去掉字符串中的 html标签
1.去除单个HTML标记
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML标记
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{ ITjob 远标教育
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签
return htmlStr.trim(); //返回文本字符串
}
}
❹ 用java字符串方法去除HTML代码标签的问题
可以通过replaceAll方法进行字符串替换,之后替换的内容用正则表达式来匹配。举例
String ss="<div id='mini_nav_qq'><li><a target='_top' " +
"href='http:// lady.qq.com/emo/emotio.shtml'>情感</a></li><li>" +
"<a target='_top' href='http://lady.qq.com/beauty/beauty.shtml'>美容</a></li></div>";
String ss=ss.replaceAll("<(/?\S+)\s*?[^<]*?(/?)>","<$1$2>");//通过只保留"<“后面的字符串,之后删除空格和后面的内容,快捷的实现去除操作(此方法通用于所有的标签去除,只需要传入不同的ss值)。
结果就是:<div><li><a>情感</a></li><li><a>美容</a></li></div>。
❺ JAVA怎样去销毁一个标签
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author daniel
*/
public class Demo extends JFrame {
private int count = 0;
Timer timer;
public Demo() {
setLayout(null);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
timer = new Timer(1000, new TimerListener());
timer.start();
}
public void mouseReleased(MouseEvent e) {
timer.stop();
{
getContentPane().removeAll();
System.gc();
}
getContentPane().repaint();
}
});
// 创建窗体
this.setTitle("按下鼠标不动信息交替出现..松开消失");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
{
getContentPane().removeAll();
System.gc();
}
getContentPane().repaint();
if ((count % 2) == 0) {
JLabel lal_1 = new JLabel("JAVA is fun");
lal_1.setSize(100, 100);
getContentPane().add(lal_1);
lal_1.setLocation(100, 100);
lal_1.setVisible(true);
} else {
JLabel lal_2 = new JLabel("JAVA is powerful");
lal_2.setSize(100, 100);
getContentPane().add(lal_2);
lal_2.setLocation(250, 100);
lal_2.setVisible(true);
}
count += 1;
}
}
public static void main(String[] args) {
Demo d = new Demo();
}
}
java的特点是“永远不需销毁对象”,也似乎没有手动销毁对象的机制。它有一个垃圾回收站,当一个对象出了作用域,且引用数为0时,就可能被垃圾回收。System.gc()用于强制进行终结动作,可加速清理。
为了能够及时清理某个对象,就将它的作用域设置得尽量小。将对象作为类成员的话,作用域太大,要等类销毁时才有机会被回收。
如果觉得getContentPane().removeAll()会同时remove掉其它对象的话,将lal_1和lal_2用一个小一点的容器装起来。
❻ java去除文本内容的标签跟
这个方法是替换内容,最后trim方法是,清除字符串两边的空格
String newStr =str.replaceAll("<p>", "").replaceAll("</p>", "").trim();
❼ java去掉字段中的html标签
用正则表达式吧,应该比较简单。
或者使用笨点的方法,循环查找'>'符号的位置,判断下一个字符是不是'<',如果是,则继续循环,如果不是则是需要留下的文本了,把文本用list保存起来继续循环直到全部字段结束。
最后list里面就是你要留下的文本了
❽ 用java去除掉这段代码的HTML标签
public static String HtmlText(String inputString) {
String htmlStr = inputString; //含html标签的字符串
String textStr ="";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); //过滤script标签
p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); //过滤style标签
p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //过滤html标签
/* 空格 —— */
// p_html = Pattern.compile("\\ ", Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = htmlStr.replaceAll(""," ");
textStr = htmlStr;
}catch(Exception e) {
}
return textStr;
}
传你的字符串进去看看,可以的话加分,谢谢
❾ 在java图形界面中如何删除已定义的标签
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
public class LabelDelTest extends JFrame implements ActionListener {
JLabel label;
JButton button;
public LabelDelTest() {
label = new JLabel("新建标签",JLabel.CENTER);
label.setBorder(new TitledBorder(""));
button = new JButton("删除标签");
button.addActionListener(this);
add(label,"North");
add(button,"South");
setTitle("标签删除");
setBounds(200,100,500,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LabelDelTest();
}
public void actionPerformed(ActionEvent e) {
remove(label);
this.repaint();
}
}
❿ java 利用jsoup 如何去除一段代码中的所有html标签,只留纯文本
这是文档,很简单的看看就懂了
网页链接