Dom4j解读XML

  
  
  
  
  1. File inputXml=new File("D:\\dconf.xml");  
  2.              
  3.             org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();   
  4.              
  5.             org.dom4j.Document document = saxReader.read(inputXml);   
  6.              
  7.             org.dom4j.Element dataRoot = document.getRootElement();  
  8.              
  9.              
  10.             for(Iterator i = dataRoot.elementIterator(); i.hasNext();){    
  11.                 org.dom4j.Element country = (org.dom4j.Element) i.next();   
  12.                  
  13.                 String id = country.attributeValue("id"); 
  14.                 String name = country.attributeValue("name"); 
  15.                  
  16.                 System.err.println("id:"+id + "  name:"+name ); 
  17.                  
  18.                  
  19.                 // 地区 
  20.                 TripDomesticLocationInfo cInfo = new TripDomesticLocationInfo(); 
  21.                  
  22.                 cInfo.setLocationId(Long.valueOf(id)); 
  23.                 cInfo.setChineseName(name); 
  24.                 cInfo.setJapaneseName(name); 
  25.                 cInfo.setLocationLevel(5); 
  26.                 cInfo.setSuperId(Long.valueOf(0)); 
  27.                  
  28.                 this.tripDomesticTravelMgr.save(cInfo); 
  29.                  
  30.                 for(Iterator j = country.elementIterator(); j.hasNext();){    
  31.                     org.dom4j.Element city = (org.dom4j.Element) j.next();   
  32.                      
  33.                     String cId = city.attributeValue("id"); 
  34.                     String cName = city.getText(); 
  35.                      
  36.                     System.err.println("cId:"+cId + "  cName:"+cName ); 
  37.                      
  38.                     // 城市 
  39.                     TripDomesticLocationInfo cityInfo = new TripDomesticLocationInfo(); 
  40.                      
  41.                     cityInfo.setLocationId(Long.valueOf(cId)); 
  42.                     cityInfo.setChineseName(cName); 
  43.                     cityInfo.setJapaneseName(cName); 
  44.                     cityInfo.setLocationLevel(4); 
  45.                     cityInfo.setSuperId(cInfo.getLocationId()); 
  46.                     this.tripDomesticTravelMgr.save(cityInfo); 
  47.                 }    
  48.                 System.err.println("================================"); 
  49.                  
  50.                  
  51.             } 

 

你可能感兴趣的:(xml,解析,dom4j,解析xml,休闲)