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<Element> listItemNet = doc.selectNodes("/root/net/item");
			if(listItemNet !=null && listItemNet.size()>00){
				List<Item> listNet = new ArrayList<Item>();
				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<Element> listItemAutostart = doc.selectNodes("/root/autostart/item");
			if(listItemAutostart != null && listItemAutostart.size()>0){
				List<Item> listAutostart = new ArrayList<Item>();
				for(Element ele:listItemAutostart){
					Item item = new Item();
					item.setStartpath(ele.valueOf("@startpath"));
					listAutostart.add(item);
				}
				report.setListAutostart(listAutostart);
			}
			
			List<Element> listItemReleasefile = doc.selectNodes("/root/releasefile/item");
			if(listItemReleasefile != null && listItemReleasefile.size()>0){
				List<Item> listReleasefile = new ArrayList<Item>();
				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<Element> listItemBasicaction = doc.selectNodes("/root/basicaction/item");
			if(listItemBasicaction != null && listItemBasicaction.size()>0){
				List<Item> listBasicaction = new ArrayList<Item>();
				for(Element ele:listItemBasicaction){
					Item item = new Item();
					item.setProcessname(ele.valueOf("@processname"));
					List<Element> listOperateBasic = doc.selectNodes("/root/basicaction/item["+i+"]/operate");
					item.setOperateList(getOperate(listOperateBasic));
					listBasicaction.add(item);
					i++;
				}
				report.setListBasicaction(listBasicaction);
			}
			
			
			int j=1;
			List<Element> listItemInjectaction = doc.selectNodes("/root/injectaction/item");
			if(listItemInjectaction != null && listItemInjectaction.size()>0){
				List<Item> listInjectaction = new ArrayList<Item>();
				for(Element ele:listItemInjectaction){
					Item item = new Item();
					item.setProcessname(ele.valueOf("@processname"));
					List<Element> 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 version="1.0" encoding="utf-8" standalone="yes"?>
<root> 
  <net>
    <item host="127.0.0.1" port="3306" />
    <item host="www.baidu.com" port="8888" />
  </net>

  <autostart>
    <item  startpath="g:/hh.xml"/>
    <item  startpath="c:/file.exe"/> 	
  </autostart>

   <releasefile>
    <item name="c:\windows\temp\scs1.tmp  " length="" md5="" fileproperty="0"/>
    <item name="c:\windows\temp\scs2.tmp  " length="" md5="" fileproperty="0"/>
  </releasefile>

  <basicaction>
    <item processname="c:\windows\system32\drwtsn32.exe">
      <operate name="asdft" parameter="目标进程名:c:\windows\system32\ntvdm.exe"/>
	   <operate name="对对对cddad" parameter="测试的文件名:d:\test\helloword.exe"/>
    </item>
	  <item processname="d:\linux\system64\hyal64.exe">
      <operate name="你多或少的线程context" parameter="测试的阿萨德发名:c:\windows\system32\ntvdm.exe"/>
	   <operate name="拉动了房价阿里ddd" parameter="ad发放名:d:\test\helloword.exe"/>
    </item>
  </basicaction>

  <injectaction>
  	<item processname="ninini">
      <operate name="haohaohao" parameter="memememe" />
      <operate name="aiaiaiaiai" parameter="lzlzlzlzlz" />  	
    </item>
   <item processname="wowowo">
      <operate name="zaizaizai" parameter="canshucashu" />
      <operate name="nenenenen" parameter="parparpar" />  	
    </item>
  </injectaction>
</root>

 

你可能感兴趣的:(reader)