加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml字符串格式化

发布时间:2020-12-16 05:59:27 所属栏目:百科 来源:网络整理
导读:/**bb rn(+t)/bb rn/b/b rn(-t)/b rn//b rn(-t) */public static void show(String xml) {Matcher mth = Pattern.compile("(?:w+s*w+|/w+s*.|/s*.)").matcher(xml);int count = 0;StringBuffer sb = new StringBuffer();char[] cs
	/**
	<b><b>    rn(+t)
	</b><b>   rn
	</b></b>  rn(-t)
	/><b>     rn
	/></b>    rn(-t)
	 */
	public static void show(String xml) {
		Matcher mth = Pattern.compile("(?:<w+>s*<w+|</w+>s*<.|/>s*<.)").matcher(xml);
		int count = 0;
		StringBuffer sb = new StringBuffer();
		char[] cs = "tttttttttttttttt".toCharArray();
		int lastEnd = 0;
		while (mth.find(lastEnd)) {
			try {
				String str = mth.group();
				int idx2 = str.indexOf('<',1);
				if (str.charAt(0) == '<' && str.charAt(1) != '/') {
					count++;
				} else if (str.charAt(0) == '<' && str.charAt(1) == '/') {
					if (str.charAt(idx2 + 1) == '/') {
						count--;
					}
				} else if (str.charAt(0) == '/') {
					if (str.charAt(idx2 + 1) == '/') {
						count--;
					}
				}
				sb.append(xml,lastEnd,mth.start() + str.indexOf('>') + 1).append("rn").append(cs,count);
				lastEnd = mth.start() + idx2;
			} catch (Exception e) {
				throw new RuntimeException(count + "," + lastEnd + "," + sb + "--",e);
			}
		}
		sb.append(xml,xml.length());
		System.out.println(sb);
	}


(?:) 非获取匹配,不进行存储供以后使用,预查消耗字符

(?=) 正向预查,预查不消耗字符,

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读