dom4J 递归解析

dom4J 递归解析

book.xml:
<? xml version = " 1.0 "  encoding = " UTF-8 " ?>
< INVENTORY name = " INAtrabute " >
    
< BOOK name = " textBook " >
        
< TITLE >
            The Adventures of Huckleberry Finn
            
< FEN > mast
                
< MIAO >
                miao
                
</ MIAO >
            
</ FEN >
        
</ TITLE >
        
< AUTHOR > Mark Twain
            
< FEN > mast1
                
< MIAO >
                miao1
                
</ MIAO >
            
</ FEN >
        
</ AUTHOR >
        
< BINDING > mass market paperback </ BINDING >
        
< PAGES > 298 </ PAGES >
    
< price > 209yuan </ price >
    
</ BOOK >
    
< BOOK >
        
< TITLE name = " testName " > Leaves of Grass </ TITLE >
        
< AUTHOR > Walt Whitman </ AUTHOR >
        
< BINDING > hardcover </ BINDING >
        
< PAGES > 462 </ PAGES >
    
</ BOOK >
</ INVENTORY >

解析代码:
@Test
    
public   void  treeWalk()  throws  DocumentException {
        SAXReader reader 
= new SAXReader(); 
        Document document 
= reader.read(new File("src/book.xml"));
        Element element
= document.getRootElement();
        treeWalk(element);
        
    }

    
public   void  treeWalk(Element element) {
        
for(int i = 0 , size = element.nodeCount();i<size;i++){
            Node node 
= element.node(i);
            
            
if(node instanceof Element){
                treeWalk((Element)node);
                String value 
= node.getName();
                System.out.println(value);
            }
else{
                
            }

        }

    }

你可能感兴趣的:(dom4J 递归解析)