Dom4jXPath综合应用

来源:互联网 发布:域名每年都要交钱吗 编辑:程序博客网 时间:2024/05/21 10:43

Dom4j XPath 综合应用

案例

            org.dom4j.Document doc;            try {                doc = DocumentHelper.parseText(requestContent);            } catch (DocumentException e1) {                e1.printStackTrace();                throw new ClientProtocalServiceException("处理Syncml XMl生成doc异常",e1);            }            Map<String, String> nsmap = new HashMap<String, String>();            String nsURI = doc.getRootElement().getNamespaceURI();            nsmap.put("xmlns", nsURI);            org.dom4j.XPath xMsgID=doc.createXPath("//xmlns:SyncML/xmlns:SyncHdr/xmlns:MsgID");            xMsgID.setNamespaceURIs(nsmap);            String strMsgID=xMsgID.selectSingleNode(doc).getStringValue();            //如果为第二回合客户端所发消息,则验证其是否获得通道            Boolean isNeedUpdateChannel=false;//是否需要对channel进行更新,若为true则更新            if(strMsgID.equals("2")){                Boolean isGetChannel=false;//是否已经获取channel                //不管当前path位置从根路径进行查                org.dom4j.XPath x = doc.createXPath("//xmlns:SyncML/xmlns:SyncBody/xmlns:Results");                x.setNamespaceURIs(nsmap);                List<Node>nodes=x.selectNodes(doc);                Map<String,Node> checkChannelMap=new HashMap<String,Node>();                for(Node node:nodes){                    org.dom4j.XPath xCmdRef=node.createXPath("xmlns:CmdRef");                    org.dom4j.XPath xItem=node.createXPath("xmlns:Item");                    xCmdRef.setNamespaceURIs(nsmap);                    xItem.setNamespaceURIs(nsmap);                    String cmdRef=xCmdRef.selectSingleNode(node).getStringValue();                    Node item=xItem.selectSingleNode(node);                    checkChannelMap.put(cmdRef, item);                }                Node configurationStatusNode=checkChannelMap.get("3");                Node channelStatusNode=checkChannelMap.get("5");                //从当前path查询元素                org.dom4j.XPath xConfigurationStatusData=configurationStatusNode.createXPath("xmlns:Data");                org.dom4j.XPath xChannelStatusNodeData=channelStatusNode.createXPath("xmlns:Data");                String strConfigurationStatusData = xConfigurationStatusData.selectSingleNode(configurationStatusNode).getStringValue();                String strChannelStatusNodeData = xChannelStatusNodeData.selectSingleNode(channelStatusNode).getStringValue();                if(strConfigurationStatusData.equals("1") && strChannelStatusNodeData.equals("1")){                    isGetChannel=true;                }                //比较日期看是否需要对channel进行更新                if(isGetChannel==true){                    Node nodeExpirationTime=checkChannelMap.get("6");                    org.dom4j.XPath xExpirationTimeData=nodeExpirationTime.createXPath("xmlns:Data");                    xExpirationTimeData.setNamespaceURIs(nsmap);                    String expirationTime = xExpirationTimeData.selectSingleNode(nodeExpirationTime).getStringValue().split("\\.")[0];                    DateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");                    long days=0;                    try{                        Date d1=df.parse(expirationTime);                        Date d2=new Date();                        long diff = d1.getTime() - d2.getTime();                        days = diff / (1000 * 60 * 60 * 24);                    }catch(Exception e){                        throw new ClientProtocalServiceException("channel过期日期与当前日期比较发生异常",e);                    }                    //若距离channel过期时间在5天之内则对通道进行更新                    if(days<5){                        isNeedUpdateChannel=true;                    }else{                        //channel存在且未过期(距离过期时间大于等于5天),将channel取出并更新到数据库相应字段中                        Node nodeChannel=checkChannelMap.get("4");                        org.dom4j.XPath xNodeChannel=nodeChannel.createXPath("xmlns:Source/xmlns:LocURI");                        xNodeChannel.setNamespaceURIs(nsmap);                        String channel=xNodeChannel.selectSingleNode(nodeChannel).getStringValue();                        Boolean isChannelUpdateSuccess=deviceService.updateWindowsChannel(imei, channel);                        if(isChannelUpdateSuccess==false){                            throw new ClientProtocalServiceException("windows 推送channel未能成功更新到数据库中");                        }                    }                }else{                    //未获取channel的情况也需要对channel进行更新                    isNeedUpdateChannel=true;                }            }            if(isNeedUpdateChannel==true){                try{                    Velocity.init();                     VelocityContext context = new VelocityContext();                     context.put("DeviceID", "0");                     context.put("SessionID", "1");                     context.put("MsgID", "2");                     Reader reader = new FileReader(new File("vm/UpdateConfiguration.xml"));                     StringWriter writer = new StringWriter();                     Velocity.evaluate(context, writer, "", reader);                     return XMLToSingleLineUtil.putXmlIntoALine(writer.toString());                }catch(Exception e){                    throw new ClientProtocalServiceException("处理UpdateConfiguration,即更新channel发生异常",e);                }            }
0 0