原始xml内容:
1
2
3 b1
4
5 123
6
7
8
9
10 11
可用下面的代码去掉 、这二个空节点 (注 中间有一个空格,不算空节点)
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() <= 0) {
12 break;
13 }
14 for (Element e : list) {
15 System.out.println(e.getName());
16 e.getParent().remove(e);
17 }
18 }
19 System.out.println(document.asXML());
20
21 }
以上代码使用了dom4j
输出内容:
awbno
spl
spls
b1123
即:
1
2
3 b1
4
5 123
6
7