首页 > IT职业 > IT职场 > java:利用xpath删除xml中的空节点
原始xml内容:
1 2 3 b1 45 8123 67 9 1110
可用下面的代码去掉
1 @Test
2 public void testDeleteEmptyNode() throws DocumentException {
3 String xmlOrigin = "b1123 ";
4 SAXReader saxReader = new SAXReader();
5 Document document = saxReader.read(new ByteArrayInputStream(xmlOrigin
6 .getBytes()));
7
8 while (true) {
9 @SuppressWarnings("unchecked")
10 List list = document.selectNodes("//*[not(node())]");
11 if (list == null || list.size()
以上代码使用了dom4j
输出内容:
即:
1
2
3 b1
4
5 123
6
7
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4022/viewspace-2800251/ ,如需转载,请注明出处,否则将追究法律责任。