dom4j解析xml

待解析的xml文如下:

View Code
<root>
<requestContent>
    <requestFrom></requestFrom>
        <logNeId></logNeId>
        <areaCode></areaCode>
        <servCode></servCode>
        <operateId></operateId>
        <synTimeOut></synTimeOut>
        <cmdpParas>
            <cmdPara>
                <fromParaCode></fromParaCode>
                <paraValue></paraValue>
            </cmdPara>
            <cmdPara>
                <fromParaCode></fromParaCode>
                <paraValue></paraValue>
            </cmdPara>
        </cmdParas>
    </requestContent>
</root>

解析如下:

/**
     * 解析请求xml<br>
     * @param xml 请求的xml
    * @return String由错误编码和错误描述拼接而成
     */
      public  String readStringXml(String xml){
          NeopRequest neopRequest =new NeopRequest();
          String synTimeOut=null;//超时时间
          Document doc = null;
          try {
            doc = DocumentHelper.parseText(xml);// 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
            Iterator iter = rootElt.elementIterator("requestContent"); // 获取根节点下的子节点requestContent
            while(iter.hasNext()){
                Element recordEle = (Element) iter.next();
                String requestFrom = recordEle.elementTextTrim("requestFrom"); // 拿到requestContent节点下的子节点requestFrom值
                neopRequest.setRequestFrom(requestFrom);//请求来源标识
                logger.info("requestFrom:" + requestFrom);
                String logNeId = recordEle.elementTextTrim("logNeId"); // 拿到requestContent节点下的子节点logNeId值
                logger.info("logNeId:" + logNeId);
                neopRequest.setLogicNeId(Long.parseLong(logNeId));//逻辑网元id
                
                String areaCode = recordEle.elementTextTrim("areaCode"); // 拿到requestContent节点下的子节点areaCode值
                logger.info("areaCode:" + areaCode);
                neopRequest.setLocalAreaCode(areaCode); //本地网编码
                
                String servCode = recordEle.elementTextTrim("servCode"); // 拿到requestContent节点下的子节点servCode值
                logger.info("servCode:" + servCode);
                
                neopRequest.setListCmdTemplateId(SouthInvokService.getCmdTemplateIds(Long.parseLong(logNeId), servCode)); //命令模版ID,用来匹配指令模版
                
                
                String operateId = recordEle.elementTextTrim("operateId"); // 拿到requestContent节点下的子节点operateId值
                logger.info("operateId:" + operateId);
                neopRequest.setRequestId(operateId);//操作id, 对应每次请求的序列号
                

                synTimeOut = recordEle.elementTextTrim("synTimeOut"); // 拿到requestContent节点下的子节点synTimeOut值,超时时间
                logger.info("synTimeOut:" + synTimeOut);
                
                Element cmdParas = recordEle.element("cmdParas"); //cmdParas节点
                Iterator it = cmdParas.elementIterator("cmdPara");
                 while(it.hasNext()){
                    Element e = (Element) it.next();
                    Map<String, String> mapCmdPara = new HashMap<String, String>();
                    String fromParaCode=e.elementTextTrim("fromParaCode");
                    String paraValue=e.elementTextTrim("paraValue");
                    logger.info("上游系统编码"+fromParaCode+"对应的参数值是:"+paraValue);
                    neopRequest.setCmdPara(mapCmdPara);//命令参数
                 }
            }
        } catch (DocumentException e) {
            logger.error("xml解析异常!", e);
          }
        long t1 = System.currentTimeMillis();
        NeopResponse neopResponse=submitRequest(neopRequest,synTimeOut);
        long t2 = System.currentTimeMillis();
        long realSpendTime = t2 - t1;
        logger.info("【耗时】: " + realSpendTime + " 毫秒");
        //返回响应的xml
        return createXml(neopResponse);
      }

 

posted @ 2013-01-23 10:51  合肥房屋托管  阅读(313)  评论(0编辑  收藏  举报