关于用jsoup解析出来的html文档内容中,对其中的括号替换文本框问题,求高手解答!

仲少 发布于 2013/08/22 12:00
阅读 1K+
收藏 0

我用jsoup把html中的内容解析出来了,但是不懂怎么把内容中的括号换成文本框,求高手帮忙!

import java.io.File;
import java.io.IOException;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

public class Test5 {
	public static void main(String[] args) throws IOException{
		
		
		File input = new File("D:\\input5.html");
		Document doc = Jsoup.parse(input, "GBK");		           

		Elements title1 = doc.select(".Title1");
		Elements title2 = doc.select(".Title2");
		Elements answer1 = doc.select("p.Answer1CharChar");

		for (Element link: title1){
			String text = link.text();
			System.out.println(text);		
		}
		for (Element link: title2){
			String text = link.text();
			System.out.println(text);		
		}
		for (Element link: answer1){
			String text = link.text();
			System.out.println(text);
	    }
    }
}

解析出来的效果如图:就是想把其中的括号和答案换成文本框!该怎么换呢??

HTML代码是这样的:

<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=x-cp20936">
<meta name=Generator content="Microsoft Word 11 (filtered)">
<title>二、选择合适的字填入括号内</title>
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:宋体;
	panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
	{font-family:"\@宋体";
	panose-1:2 1 6 0 3 1 1 1 1 1;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:"Times New Roman";}
p.MsoHeader, li.MsoHeader, div.MsoHeader
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	layout-grid-mode:char;
	border:none;
	padding:0cm;
	font-size:9.0pt;
	font-family:"Times New Roman";}
p.MsoFooter, li.MsoFooter, div.MsoFooter
	{margin:0cm;
	margin-bottom:.0001pt;
	layout-grid-mode:char;
	font-size:9.0pt;
	font-family:"Times New Roman";}
p.p0, li.p0, div.p0
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.5pt;
	font-family:"Times New Roman";}
p.Title1, li.Title1, div.Title1
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:16.0pt;
	font-family:"Times New Roman";}
p.Title2, li.Title2, div.Title2
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:16.0pt;
	font-family:"Times New Roman";}
p.Title3, li.Title3, div.Title3
	{margin:0cm;
	margin-bottom:.0001pt;
	line-height:17.8pt;
	background:white;
	font-size:16.0pt;
	font-family:宋体;}
p.Answer1, li.Answer1, div.Answer1
	{margin:0cm;
	margin-bottom:.0001pt;
	line-height:17.8pt;
	background:white;
	font-size:16.0pt;
	font-family:宋体;}
span.p0Char
	{font-family:宋体;}
span.Answer1CharChar
	{font-family:宋体;}
p.Answer2, li.Answer2, div.Answer2
	{margin:0cm;
	margin-bottom:.0001pt;
	line-height:17.8pt;
	background:white;
	font-size:16.0pt;
	font-family:宋体;}
span.Answer2Char
	{font-family:宋体;}
p.Title4, li.Title4, div.Title4
	{margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:16.0pt;
	font-family:"Times New Roman";}
 /* Page Definitions */
 @page Section1
	{size:595.3pt 841.9pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	layout-grid:15.6pt;}
div.Section1
	{page:Section1;}
-->
</style>

</head>

<body bgcolor=white lang=ZH-CN style='text-justify-trim:punctuation'>

<div class=Section1 style='layout-grid:15.6pt'>

<p class=Title1><span style='font-family:宋体'>1、下面句子不是比喻句的一句是(</span><span
class=Answer1CharChar>C</span><span style='font-family:宋体'>)</span></p>

<p class=Title2><span style='font-family:宋体'>A.它们肃立不动,像是为老人守灵的白翼天使。</span></p>

<p class=Title2><span style='font-family:宋体'>B.我们好不容易才从这片飞动的白色漩涡中脱出身来。</span></p>

<p class=Title2><span style='font-family:宋体'>C.海鸥的叫声和姿态与平时大不一样,像是发生了什么大事。</span></p>

<p class=Title2><span style='font-family:宋体'>D.海鸥们轮流飞到老人遗像前的空中,像是前来瞻仰遗容的亲属。</span></p>

</div>

</body>

</html>

加载中
0
iSea
iSea
html的关键代码贴出来看一下哦。。。
仲少
仲少
大师已经附上html代码在问题描述中了!求指导!
0
Beyond-Bit
Beyond-Bit

java jdk的 replace   酌情选择具体函数替换“<input type="text"/>”即可

只要你没有过滤,jsp页面解析的时候会解析成输入框的、

0
平凡男
平凡男
public static void replaceHtml() {
        File input = new File("e:/test.txt");
         try {
            Document doc = Jsoup.parse(input,"gbk");
            Element body = doc.body();
            
            Elements items = body.select("div.Section1>p");
            Element firstItem = items.first();
            String letter = firstItem.child(1).text();
            for (int i=1, j=items.size(); i<j; i++) {
                Element item = items.get(i);
                String answerText = item.child(0).text();
                String answerLetter = answerText.substring(0,1);
                if(letter.equals(answerLetter)) {
                    firstItem.child(1).html(answerText.substring(2));
                }
            }
            System.out.println(body.html());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
仲少
仲少
你这个方法是取出div之间的内容!我的意思是把题目里面的括号用文本框来替换!在题目呈现出来的时候,那个括号变成了文本框!该怎么替换呢?
仲少
仲少
那我上面写的那个main方法怎么实现对它的使用呢?
0
平凡男
平凡男
public static void main(String[] args) {
        replaceHtml();
    }
    
    public static void replaceHtml() {
        File input = new File("e:/test.txt");
         try {
            Document doc = Jsoup.parse(input,"gbk");
            Element body = doc.body();
            
            Elements items = body.select("div.Section1>p");
            Element firstItem = items.first();
            String letter = firstItem.child(1).text();
            for (int i=1, j=items.size(); i<j; i++) {
                Element item = items.get(i);
                String answerText = item.child(0).text();
                String answerLetter = answerText.substring(0,1);
                if(letter.equals(answerLetter)) {
                    String textTag = "<input type='text' value='" + answerText.substring(2) + "' />";
                    firstItem.child(1).html(textTag);
                }
            }
            System.out.println(body.html());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
仲少
仲少
这个方法还是调用了div标签之间的内容喔!
0
平凡男
平凡男
哎,无语了
仲少
仲少
呵呵。。。初学者,所以很懂问题还不懂!
0
平凡男
平凡男
public static void replaceHtml() {
        File input = new File("e:/test.txt");
         try {
            Document doc = Jsoup.parse(input,"gbk");
            Element body = doc.body();
            
            Elements items = body.select("div.Section1>p");
            Element firstItem = items.first();
            String letter = firstItem.child(1).text();
            for (int i=1, j=items.size(); i<j; i++) {
                Element item = items.get(i);
                String textTag = "<input type='text' value='' />";
                firstItem.child(1).html(textTag);
            }
            System.out.println(body.html());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
0
平凡男
平凡男
多看看api吧
仲少
仲少
嗯,谢谢!!
返回顶部
顶部