ITPub博客

首页 > IT职业 > IT职场 > java:利用xpath删除xml中的空节点

java:利用xpath删除xml中的空节点

IT职场 作者:jhere 时间:2021-09-09 09:11:30 0 删除 编辑

java:利用xpath删除xml中的空节点

原始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() 


以上代码使用了dom4j

 

输出内容:

awbno
spl
spls

b1123

即:


1 
2   
3   b1
4   
5     123
6   
7 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4022/viewspace-2800251/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-17

  • 博文量
    81
  • 访问量
    656