SAXReader读取xml(属性value有值)

	public Report getReportByXml(String xmlPath) {
		File file = new File(xmlPath);
		FileInputStream fis = null;
		SAXReader saxReader = new SAXReader();
		Document doc = null;
		Report report = null;
		try {
			report = new Report();
			fis = new FileInputStream(file);
			doc = saxReader.read(new InputStreamReader(fis, "UTF-8"));
			List listItemNet = doc.selectNodes("/root/net/item");
			if(listItemNet !=null && listItemNet.size()>00){
				List listNet = new ArrayList();
				for(Element ele:listItemNet){
					Item item = new Item();
					item.setHost(ele.valueOf("@host"));
					item.setPort(ele.valueOf("@port"));
					listNet.add(item);
				}
				report.setListNet(listNet);
			}
			
			List listItemAutostart = doc.selectNodes("/root/autostart/item");
			if(listItemAutostart != null && listItemAutostart.size()>0){
				List listAutostart = new ArrayList();
				for(Element ele:listItemAutostart){
					Item item = new Item();
					item.setStartpath(ele.valueOf("@startpath"));
					listAutostart.add(item);
				}
				report.setListAutostart(listAutostart);
			}
			
			List listItemReleasefile = doc.selectNodes("/root/releasefile/item");
			if(listItemReleasefile != null && listItemReleasefile.size()>0){
				List listReleasefile = new ArrayList();
				for(Element ele:listItemReleasefile){
					Item item = new Item();
					item.setName(ele.valueOf("@name"));
					item.setLength(ele.valueOf("@length"));
					item.setMd5(ele.valueOf("@md5"));
					item.setFileproperty(ele.valueOf("@fileproperty"));
					listReleasefile.add(item);
				}
				report.setListReleaaseFile(listReleasefile);
			}
			
			int i = 1;
			List listItemBasicaction = doc.selectNodes("/root/basicaction/item");
			if(listItemBasicaction != null && listItemBasicaction.size()>0){
				List listBasicaction = new ArrayList();
				for(Element ele:listItemBasicaction){
					Item item = new Item();
					item.setProcessname(ele.valueOf("@processname"));
					List listOperateBasic = doc.selectNodes("/root/basicaction/item["+i+"]/operate");
					item.setOperateList(getOperate(listOperateBasic));
					listBasicaction.add(item);
					i++;
				}
				report.setListBasicaction(listBasicaction);
			}
			
			
			int j=1;
			List listItemInjectaction = doc.selectNodes("/root/injectaction/item");
			if(listItemInjectaction != null && listItemInjectaction.size()>0){
				List listInjectaction = new ArrayList();
				for(Element ele:listItemInjectaction){
					Item item = new Item();
					item.setProcessname(ele.valueOf("@processname"));
					List listOperateInject = doc.selectNodes("/root/injectaction/item["+j+"]/operate");
					item.setOperateList(getOperate(listOperateInject));
					listInjectaction.add(item);
					j++;
				}
				report.setListInjectaction(listInjectaction);
			}
		} catch (DocumentException e) {
			logger.error(e.getMessage());
		} catch (FileNotFoundException e) {
			logger.error("xml is not exist! : "+xmlPath);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}finally{
				try {
					if(fis != null){
					   fis.close();
					}
				} catch (IOException e) {
				}
		}
		return report;
	}

 
  
    
    
  

  
    
     	
  

   
    
    
  

  
    
      
	   
    
	  
      
	   
    
  

  
  	
      
        	
    
   
      
        	
    
  

 

你可能感兴趣的:(XML)