!191 新增去除xml注释内容的方法

Merge pull request !191 from webhua_323/v5-dev
This commit is contained in:
Looly 2020-10-09 09:48:21 +08:00 committed by Gitee
commit c1b757fa60
2 changed files with 24 additions and 0 deletions

View File

@ -69,6 +69,10 @@ public class XmlUtil {
* 在XML中无效的字符 正则
*/
public static final String INVALID_REGEX = "[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]";
/**
* 在XML中注释的内容 正则
*/
public static final String NOTE_REGEX = "(?s)<!--.+?-->";
/**
* XML格式化输出默认缩进量
*/
@ -671,6 +675,19 @@ public class XmlUtil {
return xmlContent.replaceAll(INVALID_REGEX, "");
}
/**
* 去除XML文本中的注释内容
*
* @param xmlContent XML文本
* @return 当传入为null时返回null
*/
public static String cleanNote(String xmlContent) {
if (xmlContent == null) {
return null;
}
return xmlContent.replaceAll(NOTE_REGEX, "");
}
/**
* 根据节点名获得子节点列表
*

View File

@ -211,6 +211,13 @@ public class XmlUtilTest {
Assert.assertEquals(testBean.getBankCode(), testBean2.getBankCode());
}
@Test
public void cleanNoteTest() {
final String xmlContent = "<info><title>hutool</title><!-- 这是注释 --><lang>java</lang></info>";
final String ret = XmlUtil.cleanNote(xmlContent);
Assert.assertEquals("<info><title>hutool</title><lang>java</lang></info>", ret);
}
@Data
public static class TestBean {
private String ReqCode;