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 = ""; 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个的值, fromInx从1开始 * * @param fieldName * @param fromInx * @return */ public String decode(String fieldName, int fromInx) { String _retStr = ""; String _match = "<" + fieldName + ">"; String _match2 = ""; 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 = ""; 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 = ""; 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 equalsAttribute(String fieldName, String attribute, String value) { List result = new ArrayList(); int fromInx = 1; String _retStr = ""; String _match = "<" + fieldName+ " "; String _match2 = ""; 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 = ""; 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 = "8000040766222.73.48.192:9200\\\\20120420\\\\8000042009\\\\8000042009.rmvb2-104.rmvb9a59e568de6b7dcd4f6bf3fdd730e1cec51e5e54108095013202405486400001.0.1.268000042009NO_CONVERT57f94c55d81a672faa267d50a0b8be1dad43179d\\\\20120420\\\\8000042009\\\\1021.srt6172476e0c727a113b63a36bd33b76096689704f\\\\20120420\\\\8000042009\\\\1022.xml9edf5e5f4fb297c3807db95ba5f3c66d9a035f71\\\\20120420\\\\8000042009\\\\1023.xml31ce06e929a28f073b8ea2fd63ca484fb573ffdb\\\\20120420\\\\8000042009\\\\1024.xmlb5c59e633e8feaa3287b3c380e06c576b3b5a9f2\\\\20120420\\\\8000042009\\\\1025.xml"; 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 list = parser.equalsAttribute(fieldName, attribute, "config"); if (list.size() > 0) { Iterator 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://yuncode.net