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;
if(strMsgID.equals("2")){
Boolean isGetChannel=false;
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");
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;
}
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);
}
if(days<5){
isNeedUpdateChannel=true;
}else{
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{
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);
}
}