JAVA 解析xml

 

/**
  * 解析上传文件路径
  *
  * @return
  * @throws DocumentException
  * @throws IllegalAccessException
  */
 @SuppressWarnings("unchecked")
 private void parseXml() throws DocumentException, IllegalAccessException {

  SAXReader cgReader = new SAXReader();
  Document doc;
  try {
   doc = cgReader.read(new File(webPath));
  } catch (Exception e1) {
   // e1.printStackTrace();
   System.err.println("找不到文件,请确认文件是否存在!");
   return;
  }
  Element root = doc.getRootElement();

  // 得到上传文件路径
  Element uploadElement = root.element("server-upload-path");
  if (uploadElement == null) {
   System.err.println("请确认文件格式是否被改变,不能解析上传文件路径!");
  } else {
   // 解析绝对路径和相对路径
   Element realpathElement = uploadElement.element("realpath");
   Element absolutepathElement = uploadElement.element("absolutepath");

   if (realpathElement != null || absolutepathElement != null) {
    
    if (realpathElement != null)
    {
     String rValue = realpathElement.elementText("value");
     if (null != rValue && !"".equals(rValue)) {
      if (rValue.charAt(rValue.length()-1) == '/') {
       uploadPath =  rValue;
      }else {
       uploadPath =  rValue + "/";
      }
     }
    }
    
    if (absolutepathElement != null)
    {
     String aValue = absolutepathElement.elementText("value");
     if (null != aValue && !"".equals(aValue)) {
      uploadPath = PathUtil.getWebRoot() + aValue + "/";
     }
    }
   } else {
    System.err.println("不能解析上传文件路径!");
   }
  }

  // 得到录音文件路径
  Element tapElement = root.element("server-tap-path");

  if (tapElement == null) {
   System.err.println("请确认文件格式是否被改变,不能解析录音文件路径!");
  } else {
   // 解析绝对路径和相对路径
   Element realpathElement = tapElement.element("realpath");
   Element absolutepathElement = tapElement.element("absolutepath");

   if (realpathElement != null || absolutepathElement != null) {
    
    if (realpathElement != null)
    {
     String rValue = realpathElement.elementText("value");
     tapPath = rValue ;
    }
    
    if (absolutepathElement != null)
    {
     String aValue = absolutepathElement.elementText("value");
     
     tapWebPath = aValue;
    }
    
//    if (null != aValue && !"".equals(aValue)) {
//     tapPath = PathUtil.getWebRoot() + aValue + "/";
//
//    } else {
//     if (null != rValue && !"".equals(rValue)) {
//      tapPath = PathUtil.getWebRoot() + aValue + "/";
//     } else {
//      System.err.println("请确认文件格式是否被改变,不能解析录音文件路径!");
//     }
//    }
   }
  }
  
  // 得到上传文件格式
  Element faxTypeElement = root.element("server-fax-upload-file-type");
  
  if (faxTypeElement == null) {
   System.err.println("请确认文件格式是否被改变,不能解析可上传传真类型文件!");
  }else {
   Element allow = faxTypeElement.element("allow");
   if (allow != null) {
    List<Element> types = allow.elements("value");
    for (Element type : types) {
     if (!"".equals(type) && !list.contains(type.getText().toLowerCase()))
      list.add(type.getText().toLowerCase());
    }
   }else {
    System.err.println("请确认文件格式是否被改变,不能解析可上传传真类型文件!");
   }
  }
  
  
  // 解析oxc配置
  Element ocxElement = root.element("server-hw-ocx-config");

  if (ocxElement == null) {
   System.err.println("请确认文件格式是否被改变,不能解析server配置文件路径!");
  } else {
   List<Element> ocxConfig = ocxElement.elements();
   for (Element e : ocxConfig) {
    if (e.getName().equals("MainCcsIP")) {
     MainCcsIP = e.elementText("value");
     continue;
    }
    if (e.getName().equals("BackCcsIP")) {
     BackCcsIP = e.elementText("value");
     continue;
    }
    if (e.getName().equals("AgentType")) {
     AgentType = e.elementText("value");
     continue;
    }
    if (e.getName().equals("CardType")) {
     CardType = e.elementText("value");
     continue;
    }
    if (e.getName().equals("CcsID")) {
     CcsID = e.elementText("value");
     continue;
    }
    if (e.getName().equals("AutoAnswer")) {
     AutoAnswer =e.elementText("value");
     continue;
    }
    if (e.getName().equals("AutoRelease")) {
     AutoRelease = e.elementText("value");
     continue;
    }
    
    if (e.getName().equals("AutoReconnect")) {
     AutoReconnect = e.elementText("value");
     continue;
    }
    
    if (e.getName().equals("CalledIDForDial")) {
     CalledIDForDial = e.elementText("value");
     continue;
    }
   }
  }
  
  // 解析坐席统计参数配置
  Element arElement = root.element("server-accountreport");
  if (arElement == null) {
   System.err.println("不能解析 server-accountreport 配置文件路径!");
  } else {
   List<Element> arConfig = arElement.elements();
   for (Element e : arConfig) {
    if (e.getName().equals("CallLong")) {
     if (e.elementText("value")!=null && !"".equals(e.elementText("value")))
     CallLong = Long.parseLong(e.elementText("value"));
     continue;
    }
   }
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值