add method

This commit is contained in:
lixiaohua 2020-10-08 03:25:17 +08:00
parent 9e2e9ee230
commit 2602935b60
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 = "<title>hutool</title><!-- 这是注释 --><lang>java</lang>";
final String ret = XmlUtil.cleanNote(xmlContent);
Assert.assertEquals("<title>hutool</title><lang>java</lang>", ret);
}
@Data
public static class TestBean {
private String ReqCode;