`

字符串替换

    博客分类:
  • JSP
阅读更多

百度RSS新闻中的XML的

< description >
- <![CDATA[
<a target=_blank href=http://news.beijingoffice.com.cn/35/2008111185936.html><img border=0 src=image/20/94/339730.jpg></a><br>刚刚过去的传统楼市"银十"风光不再,在月初的秋交会和下旬出台的中央"救市"新政合捧之下,10月深圳楼市的成交并没有出现显著放量。最新出炉的研究报告显示,上月深圳一手房成交量仅上涨10%,均价12979元/平方米 ... <br /> <a href=http://tech.qq.com/a/20081111/000109.htm target=_blank style="font-size:13px">大摩报告称:国内房市将进入数年熊市</a><nobr><span style="padding-left:10px;font-size:12px;color:#666666">QQ</font></nobr><br> <a href=http://sz.house.sina.com.cn/news/2008-11-11/082721998.html target=_blank style="font-size:13px">深圳上周新房成交量翻一番 均价同步涨近一成</a><nobr><span style="padding-left:10px;font-size:12px;color:#666666">新浪</font></nobr><br>  - <a href=http://www.p5w.net/news/cjxw/200811/t2000687.htm target=_blank><font color="#008000">全景网</font></a> - <a href=http://www.house365.com/news/html/200811/79353_1.htm target=_blank><font color="#008000">365地产家居网</font></a> - <a href=http://cd.focus.cn/news/2008-11-11/562634.html target=_blank><font color="#008000">焦点房地产</font></a> - <a href=http://www.cq.xinhuanet.com/house/2008-11/11/content_14883026.htm target=_blank><font color="#008000">新华网重庆频道</font></a>  - <a href=/n?cmd=2&page=%68%74%74%70%3a%2f%2f%77%77%77%2e%63%66%69%2e%6e%65%74%2e%63%6e%2f%70%32%30%30%38%31%31%31%31%30%30%30%34%39%35%2e%68%74%6d%6c&pn=1&clk=crel&cls=housenews&where=focuspage&class=housenews><font color="#008000">524条相关&gt;&gt;</font></a><br>
  ]]> <script type="text/javascript"><!----></script>
  </ description >
目标:
1.src=image/20/94/339730.jpg替换为http://news.baidu.com/image/20/94/339730.jpg
2.最后一个<a>标签为相关信息,删除

解决办法:用下则表达式为主解决了问题一,用到了HTML Parser解决问题二

 

public String doReplace(String content) {
		String content2 = content;
		try {
			Parser myParser;
			myParser = Parser.createParser(content2, "GBK");
			NodeFilter filter = new TagNameFilter("A");
			NodeList list = myParser.extractAllNodesThatMatch(filter);
			Node tempNode = list.remove(list.size()-1);
			int startNum = tempNode.getStartPosition();
			content2 = content2.substring(0,startNum)+"<br/>";
		} catch (ParserException e) {
			e.printStackTrace();
		}
		
		
		
		Pattern p1 = Pattern.compile("<img(.*?)>", Pattern.CASE_INSENSITIVE);
		Matcher m1 = p1.matcher(content2);
		StringBuffer sb = new StringBuffer();
		while (m1.find()) {
			String tempImg = m1.group();
			int x = tempImg.indexOf(" src");
			// 用图片的格式来定位src结束位置
			int y = tempImg.indexOf(".jpg");
			if (y == -1) {
				y = tempImg.indexOf(".jpeg");
				if (y == -1) {
					y = tempImg.indexOf(".png");
					if (y == -1) {
						y = tempImg.indexOf(".gif");
						if (y == -1) {
							y = tempImg.indexOf(".bmp");
						} else {
							y += 4;
						}
					} else {
						y += 4;
					}
				} else {
					y += 5;
				}
			} else {
				y += 4;
			}
			if (y != -1) {
				// 去掉src处空格
				String tempSrc = tempImg.substring(x + 4, y).trim();
				if (tempSrc.startsWith("=")) {
					tempSrc = tempSrc.substring(1, tempSrc.length()).trim();
				}

				// 替换不是以http://开头的图片路径
				if (!tempSrc.startsWith("http://")) {
					tempSrc = "=http://news.baidu.com/" + tempSrc;
					tempImg = tempImg.substring(0, x + 4) + tempSrc
							+ tempImg.substring(y, tempImg.length());
				}
				// 以上代码达到了把<img >标签的src属性替换
				// -----------------------
				m1.appendReplacement(sb, tempImg);

			}

		}

		m1.appendTail(sb);
		content2 = sb.toString();
		return content2;
	}

 

分享到:
评论
Global site tag (gtag.js) - Google Analytics