java jsoup 解析字符串默認加了“/n”符號的處理

/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str="<p>供應t1紫<a href='http://www.cncu.cn' target='_blank'>銅</a>板、<a href='http://www.cncu.cn/product/tjthj_ct_zt/' target='_blank'>紫銅</a>帶,質量很優質,歡迎新老客戶前來採購。</p><p><a href='http://www.cncu.cn/product/tjthj_qt/ ' target='_blank'>紫銅帶</a>用途:高純度,組織細密,含氧量極低。無氣孔、沙眼、疏鬆,導電性能極佳,電蝕出的模具表面精度高,經熱處理工藝,電極無方向性,適合精打,細打,具有良好的熱電道性、加工性、延展性、防蝕性及耐候性等。有良好的導電、導熱、耐蝕和加工性能,可以焊接和釺焊 的<br /></p>";
		//System.out.println(processContentTest(str));
		//String str="<p>供應t1紫<a href='http://www.cncu.cn' target='_blank'>銅</a>";
		System.out.println("--------------------------------");
		System.out.println(RemoveImgAcontent(str));
	}
	public static String RemoveImgAcontent(String initcontent){
		Pattern p = Pattern.compile("</?(A|a)(\n|.)*?>");
		//Document doc = Jsoup.parseBodyFragment(initcontent); // or Jsoup.parse(...);
		Document doc = Jsoup.parseBodyFragment(initcontent);
		Elements images = doc.select("img");

		
		for(Element image : images){
			
			String altStr=image.attr("alt");
			Matcher m1 = p.matcher(altStr);
			altStr = m1.replaceAll("");
			//image.removeAttr("alt");
			image.attr("alt", altStr);
			
			String titleStr=image.attr("title");
			Matcher m2 = p.matcher(titleStr);
			titleStr = m2.replaceAll("");
			//image.removeAttr("title");
			image.attr("title", titleStr);
		}


		String endcontent=doc.select("body").html();
		return endcontent;
	}



如上例所示 默認解析後生成的內容 中會加上/n符號

看圖 做對比
解析前
f3527313-8e92-3fbe-959b-03fd464b3a77.jpg


解析後:

c31e3974-ad25-3c4d-b2e0-1ed28608ed70.jpg

看到沒多了一個 /n
查看jsoup的源碼 調試後發現 是

package org.jsoup.nodes;
....
void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) {
        String html = Entities.escape(getWholeText(), out);
        if (out.prettyPrint() && parent() instanceof Element && !Element.preserveWhitespace((Element) parent())) {
            html = normaliseWhitespace(html);
        }

        if (out.prettyPrint() && ((siblingIndex() == 0 && parentNode instanceof Element && ((Element) parentNode).tag().formatAsBlock() && !isBlank()) || (out.outline() && siblingNodes().size()>0 && !isBlank()) ))
            indent(accum, depth, out);
        accum.append(html);
    }


源碼中 該方法裏的indent方法


/**
     * 
     * @param accum
     * @param depth
     * @param out
     */
    protected void indent(StringBuilder accum, int depth, Document.OutputSettings out) {
           	accum.append("\n").append(StringUtil.padding(depth * out.indentAmount()));
    }


就是這個方法加的
看了 他的邏輯

 if (out.prettyPrint() && ((siblingIndex() == 0 && parentNode instanceof Element && ((Element) parentNode).tag().formatAsBlock() && !isBlank()) || (out.outline() && siblingNodes().size()>0 && !isBlank()) ))



out.prettyPrint() 應該是是否格式化 ,看了她的方法發現默認就是true的,
siblingIndex() 大概意思是 判斷是否根節點之類的 意思

parentNode instanceof Element 大概是判斷是否符合element格式


!isBlank()是判斷不等於空

幾個條件都是true  加起來就進入了 調用indent方法

要想不讓jsoup默認給我的字符串加入 /n
我的解決辦法是 直接註釋 indet方法的實現 讓它變成一個空殼方法

這個不是很完美,但是能解決問題

最完美的方案是 能夠重寫 該indent方法

可惜我還不清楚如何重寫

順便說下 我是從官網 把jsoup的源碼下載下來解壓到項目進行研究
這是jsoup官網 源碼包下載地址 :http://jsoup.org/download




  • f3527313-8e92-3fbe-959b-03fd464b3a77-thumb.jpg
  • 大小: 42.6 KB
  • c31e3974-ad25-3c4d-b2e0-1ed28608ed70-thumb.jpg
  • 大小: 71.7 KB
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章