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
- Dom4jXPath综合应用
- SQL综合应用学习
- J2EE综合应用
- J2EE综合应用
- MSHFlexGrid的综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- SQL综合应用学习
- T-SQL综合应用
- 信息技术综合应用系统
- 综合应用友情链接管理
- oracle函数综合应用
- 模式综合应用
- DataGridView综合应用
- 【Linux基础】综合应用
- linux awk命令详解
- 了解 Boost Filesystem Library文件系统
- Java 性能优化技巧及实战
- JAXP,sax与dom4j解析xml文档及XPath在文档内容定位中的应用(持续更新,初学者的野路子、参考和想当然,不断学习和专业化)
- image Watch: viewing in-memory images in the Visual Studio debugger
- Dom4jXPath综合应用
- 源码包安装 yum 7.4
- leetcode——96——Unique Binary Search Trees
- 疯狂互联网产品系列(一、别人的今日头条)
- 小何讲进程: Linux下进程的管理
- 今天属于老鹰乐队--加州旅馆
- Java 通过反射原理,通过Map转为Java POJO对象,适用于对象包含List,Map,数组等
- Python itertools 模块 列表元素 字符串 反向遍历合并
- 学习鸟哥的Linux私房菜基础篇总结(上)(0-9章)