Document document = DocumentHelper.parseText(XMLUtils.formatXML(xml).trim());
if (null != document) {
Element root = document.getRootElement(); // 得到根节点ToTMS
Element elemtNO = root.element("NO"); // 获得ToTMS里的NO元素
String license = elemtNO.attributeValue("NO"); // 获得NO元素里面的NO属性,获得车牌号
String message_id = elemtNO.elementText("MESSAGE_ID"); // 消息ID
String carLoc = elemtNO.elementText("LOCATION"); // 在途位置
String X = elemtNO.elementText("LOCATION_X"); // 经度
String Y = elemtNO.elementText("LOCATION_Y"); // 纬度
String haveDeaily = elemtNO.elementText("BE_DELAY"); // 是否延迟
String haveArrive = elemtNO.elementText("BE_ARRIVED"); // 是否运抵
String haveExce = elemtNO.elementText("BE_EX"); // 是否异常
String exception = elemtNO.elementText("EXCEPTION"); // 异常
String exceDesc = elemtNO.elementText("EX_DESC"); // 异常描述
}
//-----------------------------------------------------------------------------------------
public static String formatXML(String in){
if(StringUtils.isEmpty(in.trim())){
return "";
}
StringBuilder out = new StringBuilder();
in = in.trim();
int length =in.length();
char[] ch;
ch=new char[length+1];
ch=in.toCharArray() ;
for(int i=0;i<ch.length ;i++){
if(ch[i]!='\r' && ch[i]!='\t' && ch[i]!='\n'){
out.append(ch[i]);
}
}
return out.toString();
}
//-----------------------------------------------------------------------------------------
//解析报文xml
/**
<?xml version="1.0" encoding="utf-8" ?>
<ToTMS>
<NO NO="皖A66967-皖A6B68挂">
<INTERFACE_ID>GPS_TMS_LOG</INTERFACE_ID>
<MESSAGE_ID>3</MESSAGE_ID>
<SENDER>GPS</SENDER>
<RECEIVER>TMS</RECEIVER>
<LOCATION></LOCATION>
<BE_EX>False</BE_EX>
<EXCEPTION>''</EXCEPTION>
<EX_DESC>''</EX_DESC>
<BE_ARRIVED>False</BE_ARRIVED>
<BE_DELAY>False</BE_DELAY>
<LOCATION_X>117.261463</LOCATION_X>
<LOCATION_Y>31.732603</LOCATION_Y>
</NO>
</ToTMS>
*/