原始xml内容:
1 <data> 2 <a> </a> 3 <b>b1</b> 4 <awb> 5 <awbpre>123</awbpre> 6 <awbno></awbno> 7 </awb> 8 <spls> 9 <spl /> 10 </spls> 11 </data>
可用下面的代码去掉 <awbno></awbno>、<spls><spl/></spls>这二个空节点 (注<a> </a>中间有一个空格,不算空节点)
1 @Test 2 public void testDeleteEmptyNode() throws DocumentException { 3 String xmlOrigin = "<data><a> </a><b>b1</b><awb><awbpre>123</awbpre><awbno></awbno></awb><spls><spl/></spls></data>"; 4 SAXReader saxReader = new SAXReader(); 5 Document document = saxReader.read(new ByteArrayInputStream(xmlOrigin 6 .getBytes())); 7 8 while (true) { 9 @SuppressWarnings("unchecked") 10 List<Element> 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
<?xml version="1.0" encoding="UTF-8"?>
<data><a> </a><b>b1</b><awb><awbpre>123</awbpre></awb></data>
即:
1 <data> 2 <a> </a> 3 <b>b1</b> 4 <awb> 5 <awbpre>123</awbpre> 6 </awb> 7 </data>