socket xml消息包解析,socketxml消息解析,package com.
分享于 点击 44950 次 点评:280
socket xml消息包解析,socketxml消息解析,package com.
package com.ppstream.ugc.jobserver.util;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.math.NumberUtils;import com.ppstream.ugc.jobserver.core.data.JobTasksData;public class XmlPackParser { private String msg; public XmlPackParser() { } public XmlPackParser(String inMsg) { this.msg = inMsg; } public void setMsg(String inMsg) { this.msg = inMsg; } public String decode(String fieldName) { String _retStr = ""; String _match = "<" + fieldName + ">"; String _match2 = "</" + fieldName + ">"; int _nStartPos = msg.indexOf(_match) + _match.length(); int _nEndPos = msg.indexOf(_match2); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) { _retStr = this.msg.substring(_nStartPos, _nEndPos); } return _retStr; } /** * 找出第fromInx个<fieldName>的值, fromInx从1开始 * * @param fieldName * @param fromInx * @return */ public String decode(String fieldName, int fromInx) { String _retStr = ""; String _match = "<" + fieldName + ">"; String _match2 = "</" + fieldName + ">"; int _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx) + _match.length(); int _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) { _retStr = this.msg.substring(_nStartPos, _nEndPos); } return _retStr; } public int decodeInt(String fieldName) { int _nRet = -1; String _strRet = this.decode(fieldName); _strRet = _strRet.trim(); if (_strRet != null && _strRet.length() > 0) _nRet = Integer.parseInt(_strRet); return _nRet; } public int decodeInt(String fieldName, int fromIdx) { int _nRet = -1; String _strRet = this.decode(fieldName, fromIdx); _strRet = _strRet.trim(); if (_strRet != null && _strRet.length() > 0) _nRet = Integer.parseInt(_strRet); return _nRet; } public long decodeLong(String fieldName) { long _nRet = -1; String _strRet = this.decode(fieldName); _strRet = _strRet.trim(); if (_strRet != null && _strRet.length() > 0) _nRet = NumberUtils.toLong(_strRet, 0); return _nRet; } public long decodeLong(String fieldName, int fromIdx) { long _nRet = -1; String _strRet = this.decode(fieldName, fromIdx); _strRet = _strRet.trim(); if (_strRet != null && _strRet.length() > 0) _nRet = NumberUtils.toLong(_strRet); return _nRet; } public Date decodeDate(String fieldName) { Date _retDate = null; String _strDate = this.decode(fieldName); try { SimpleDateFormat _df = new SimpleDateFormat("yyyyMMddHHmm"); _retDate = _df.parse(_strDate); } catch (Exception ex) { ex.printStackTrace(); } return _retDate; } public boolean decodeBool(String fieldName) { boolean _bRet = false; String _strRet = this.decode(fieldName); _strRet = _strRet.trim(); if (_strRet != null && _strRet.length() > 0) _bRet = Boolean.parseBoolean(_strRet); return _bRet; } public boolean check(String fieldName) { String _match = "<" + fieldName + ">"; String _match2 = "</" + fieldName + ">"; int _nStartPos = msg.indexOf(_match) + _match.length(); int _nEndPos = msg.indexOf(_match2); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) return true; return false; } public boolean check(String fieldName, int fromIdx) { String _match = "<" + fieldName + ">"; String _match2 = "</" + fieldName + ">"; int _nStartPos = msg.indexOf(_match, fromIdx) + _match.length(); int _nEndPos = msg.indexOf(_match2, fromIdx); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) return true; return false; } /** * 判断指定节点下的属性与值是否匹配 * 能够在多个相同节点不同属性、不同属性值得情况下,找到需要的节点 * 返回匹配的多个节点内容 * @param fieldName 指定的节点 * @param attribute 指定的属性 * @param value 指定的属性值 * @return 返回匹配的多个节点内容 */ public List<String> equalsAttribute(String fieldName, String attribute, String value) { List<String> result = new ArrayList<String>(); int fromInx = 1; String _retStr = ""; String _match = "<" + fieldName+ " "; String _match2 = "</" + fieldName + ">"; int _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx); int _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx); while (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) { _retStr = this.msg.substring(_nStartPos, _nEndPos); int exist = _retStr.indexOf(attribute + "=\\"" + value + "\\""); if (exist != -1) { result.add(_retStr); } fromInx++; _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx); _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx); } return result; } /** * 从给定的字符串中获取指定节点的内容 * @param submsg 给定的字符串 * @param fieldName 指定的节点 * @return 指定节点的内容 */ public String decodeSubmsg(String submsg, String fieldName) { String _retStr = ""; if (StringUtils.isNotBlank(submsg)) { String _match = "<" + fieldName + ">"; String _match2 = "</" + fieldName + ">"; int _nStartPos = submsg.indexOf(_match) + _match.length(); int _nEndPos = submsg.indexOf(_match2); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) { _retStr = submsg.substring(_nStartPos, _nEndPos); } } return _retStr; } /** * 在给定的的字符串中获取指定属性的值 * @param submsg 给定的字符串 * @param attribute 属性 * @return 返回属性值 */ public String decodeAttribute(String submsg, String attribute) { String value = ""; if (StringUtils.isNotBlank(submsg)) { String _match = attribute + "=\\""; String _match2 = "\\""; int _nStartPos = submsg.indexOf(_match) + _match.length(); int _nEndPos = submsg.indexOf(_match2, _nStartPos); if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) { value = submsg.substring(_nStartPos, _nEndPos); } } return value; } public static void main(String[] args) { String testMsg = "<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?><success><video_id>8000040766</video_id><upip>222.73.48.192:9200</upip><path>\\\\20120420\\\\8000042009\\\\8000042009.rmvb</path><filename>2-104.rmvb</filename><sha1>9a59e568de6b7dcd4f6bf3fdd730e1cec51e5e54</sha1><file_size>10809501</file_size><width>320</width><height>240</height><bitrate>548</bitrate><playtime>64000</playtime><file_status>0</file_status><ugcfs_ver>1.0.1.26</ugcfs_ver><upload_id>8000042009</upload_id><special_op>NO_CONVERT</special_op><attachments><attachment type=\\"subtitle\\" attach_id=\\"1021\\"><sha1>57f94c55d81a672faa267d50a0b8be1dad43179d</sha1><path>\\\\20120420\\\\8000042009\\\\1021.srt</path></attachment><attachment type=\\"config\\" defination=\\"low\\" attach_id=\\"1022\\"><sha1>6172476e0c727a113b63a36bd33b76096689704f</sha1><path>\\\\20120420\\\\8000042009\\\\1022.xml</path></attachment><attachment type=\\"config\\" defination=\\"normal\\" attach_id=\\"1023\\"><sha1>9edf5e5f4fb297c3807db95ba5f3c66d9a035f71</sha1><path>\\\\20120420\\\\8000042009\\\\1023.xml</path></attachment><attachment type=\\"config\\" defination=\\"high\\" attach_id=\\"1024\\"><sha1>31ce06e929a28f073b8ea2fd63ca484fb573ffdb</sha1><path>\\\\20120420\\\\8000042009\\\\1024.xml</path></attachment><attachment type=\\"config\\" defination=\\"baseline\\" attach_id=\\"1025\\"><sha1>b5c59e633e8feaa3287b3c380e06c576b3b5a9f2</sha1><path>\\\\20120420\\\\8000042009\\\\1025.xml</path></attachment></attachments></success>"; XmlPackParser parser = new XmlPackParser(testMsg); String fieldName = "attachment"; String attribute = "type"; JobTasksData data = new JobTasksData(); String upip = parser.decode("upip"); System.out.println("upip = " + upip); System.out.println("//*****************************************//"); List<String> list = parser.equalsAttribute(fieldName, attribute, "config"); if (list.size() > 0) { Iterator<String> it = list.iterator(); while(it.hasNext()) { String submsg = it.next(); System.out.println(submsg); String value = parser.decodeAttribute(submsg, "defination"); System.out.println("defination = " + value);// String path = parser.decodeSubmsg(submsg, "path"); String path = data.substringHttp(parser.decodeSubmsg(submsg, "path"),parser.decode("upip")); System.out.println("path = " + path); } } }}//该片段来自于http://byrx.net
用户点评