问题产生:
在接口调用得出一个xml型字符串,一直报错
The entityname must immediately follow the '&' in the entity reference
经查发现 xml的内容里存在有 &符号 而 通过dom4j读取时 会发生错误
在xml中 “&”“<”“>”这样的标签存放在内容里是不合法的,会经常出问题。
下面找到解决方法:实测 替换 & 是可行的。
public void chartReplace(){
String str2 = "<logentry revision='1'>" +
"<msg>In this comment, I fixed a <bug>, and <added> file1&&file2.</msg>" +
"</logentry>" ;
System.out.println( "original string: " +str2);
//替换“&”:$1表示与(<msg>.*)的匹配子序列;$4表示与(.*</msg>)匹配的。
//&(?!amp;)表示匹配&而且后面不是amp;的字符串
//"$1&$3$4"得到的结果就是替换了<msg></msg>中的“&”为“&”
//由于每次只能替换掉一个“&”,所以循环执行替换,直到替换后与替换前的字符串相等。
String str1 = "" ;
while (!str2.equals(str1)){
str1 = str2;
str2 = str1.replaceAll( "(<msg>.*)(&(?!amp;))(.*</msg>)" , "$1&$3" );
}
System.out.println( "firstly replace \"&\": " +str2);
//替换“<”
str1 = "" ;
while (!str2.equals(str1)){
str1 = str2;
str2 = str1.replaceAll( "(<msg>.*)(<)(.*</msg>)" , "$1<$3" );
}
System.out.println( "then replace \"<\": " +str2);
//替换“<”
str1 = "" ;
while (!str2.equals(str1)){
str1 = str2;
str2 = str1.replaceAll( "(<msg>.*)(>)(.*</msg>)" , "$1>$3" );
}
System.out.println( "finally replace \">\": " +str2);
}
|
感谢
http://my.oschina.net/u/178218/blog/55293
相关推荐
85<br><br>0130 复制字符串中指定的字符 85<br><br>0131 巧截字符串的数字 86<br><br>0132 如何存储变长字符串 86<br><br>0133 在进行字符串比较时忽略大小写 87<br><br>0134 如何去除字符串尾空格 87<br>...
<<page 1>> page begin==================== 目 目目 目 录 录录 录 第一部分 C#语言概述.4 第一章 第一章第一章 第一章 .NET 编 编 编程语言 程语言编程语言 程语言 C#.4 1.1 Microsoft...
实例039 根据标点符号对字符串进行分行 50 实例040 判断用户输入的用户名是否正确 51 实例041 将字符串的每个字符进行颠倒输出 52 实例042 去掉字符串中的所有空格 54 实例043 从字符串中分离文件路径、文件名及 ...
1.3.9 字符串常见操作 21 1.3.10 几个常用的数学函数 27 1.4 .NET的面向对象之门 27 1.4.1 继承——“子承父业” 28 1.4.2 委托——“任务书” 35 1.4.3 事件——“年终分红” 42 1.4.4 反射——“解剖” 49 1.5 ...
1.3.9 字符串常见操作 21 1.3.10 几个常用的数学函数 27 1.4 .NET的面向对象之门 27 1.4.1 继承——“子承父业” 28 1.4.2 委托——“任务书” 35 1.4.3 事件——“年终分红” 42 1.4.4 反射——“解剖” 49 1.5 ...
9、修正“目录_是否存在”,当存在无反缀文件时返回真的BUG,感谢易友【@飞灵】反馈。 10、新增“系统_信息框Ex”定时信息框,超时后自动销毁。 效验MD5:A8EA46EB0FA7395F9355E37286B32742 精易模块 V3.86 what’s ...