[Java] java xml消息包解析 →→→→→进入此内容的聊天室

来自 , 2019-08-09, 写在 Java, 查看 114 次.
URL http://www.code666.cn/view/9246444d
  1. package com.ppstream.ugc.jobserver.util;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.List;
  8.  
  9. import org.apache.commons.lang.StringUtils;
  10. import org.apache.commons.lang.math.NumberUtils;
  11.  
  12. import com.ppstream.ugc.jobserver.core.data.JobTasksData;
  13.  
  14. public class XmlPackParser {
  15.  
  16.     private String msg;
  17.  
  18.     public XmlPackParser() {
  19.     }
  20.  
  21.     public XmlPackParser(String inMsg) {
  22.         this.msg = inMsg;
  23.     }
  24.  
  25.     public void setMsg(String inMsg) {
  26.         this.msg = inMsg;
  27.     }
  28.  
  29.     public String decode(String fieldName) {
  30.         String _retStr = "";
  31.         String _match = "<" + fieldName + ">";
  32.         String _match2 = "</" + fieldName + ">";
  33.         int _nStartPos = msg.indexOf(_match) + _match.length();
  34.         int _nEndPos = msg.indexOf(_match2);
  35.         if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) {
  36.             _retStr = this.msg.substring(_nStartPos, _nEndPos);
  37.         }
  38.         return _retStr;
  39.     }
  40.  
  41.     /**
  42.      * 找出第fromInx个<fieldName>的值, fromInx从1开始
  43.      *
  44.      * @param fieldName
  45.      * @param fromInx
  46.      * @return
  47.      */
  48.     public String decode(String fieldName, int fromInx) {
  49.         String _retStr = "";
  50.         String _match = "<" + fieldName + ">";
  51.         String _match2 = "</" + fieldName + ">";
  52.         int _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx) + _match.length();
  53.         int _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx);
  54.         if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) {
  55.             _retStr = this.msg.substring(_nStartPos, _nEndPos);
  56.         }
  57.  
  58.         return _retStr;
  59.     }
  60.  
  61.     public int decodeInt(String fieldName) {
  62.         int _nRet = -1;
  63.         String _strRet = this.decode(fieldName);
  64.         _strRet = _strRet.trim();
  65.         if (_strRet != null && _strRet.length() > 0)
  66.             _nRet = Integer.parseInt(_strRet);
  67.         return _nRet;
  68.     }
  69.  
  70.     public int decodeInt(String fieldName, int fromIdx) {
  71.         int _nRet = -1;
  72.         String _strRet = this.decode(fieldName, fromIdx);
  73.         _strRet = _strRet.trim();
  74.         if (_strRet != null && _strRet.length() > 0)
  75.             _nRet = Integer.parseInt(_strRet);
  76.         return _nRet;
  77.     }
  78.  
  79.     public long decodeLong(String fieldName) {
  80.         long _nRet = -1;
  81.         String _strRet = this.decode(fieldName);
  82.         _strRet = _strRet.trim();
  83.         if (_strRet != null && _strRet.length() > 0)
  84.             _nRet = NumberUtils.toLong(_strRet, 0);
  85.         return _nRet;
  86.     }
  87.  
  88.     public long decodeLong(String fieldName, int fromIdx) {
  89.         long _nRet = -1;
  90.         String _strRet = this.decode(fieldName, fromIdx);
  91.         _strRet = _strRet.trim();
  92.         if (_strRet != null && _strRet.length() > 0)
  93.             _nRet = NumberUtils.toLong(_strRet);
  94.         return _nRet;
  95.     }
  96.  
  97.     public Date decodeDate(String fieldName) {
  98.         Date _retDate = null;
  99.         String _strDate = this.decode(fieldName);
  100.         try {
  101.             SimpleDateFormat _df = new SimpleDateFormat("yyyyMMddHHmm");
  102.             _retDate = _df.parse(_strDate);
  103.         } catch (Exception ex) {
  104.             ex.printStackTrace();
  105.         }
  106.  
  107.         return _retDate;
  108.     }
  109.  
  110.     public boolean decodeBool(String fieldName) {
  111.         boolean _bRet = false;
  112.         String _strRet = this.decode(fieldName);
  113.         _strRet = _strRet.trim();
  114.         if (_strRet != null && _strRet.length() > 0)
  115.             _bRet = Boolean.parseBoolean(_strRet);
  116.         return _bRet;
  117.     }
  118.  
  119.     public boolean check(String fieldName) {
  120.         String _match = "<" + fieldName + ">";
  121.         String _match2 = "</" + fieldName + ">";
  122.         int _nStartPos = msg.indexOf(_match) + _match.length();
  123.         int _nEndPos = msg.indexOf(_match2);
  124.         if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos)
  125.             return true;
  126.  
  127.         return false;
  128.     }
  129.  
  130.     public boolean check(String fieldName, int fromIdx) {
  131.         String _match = "<" + fieldName + ">";
  132.         String _match2 = "</" + fieldName + ">";
  133.         int _nStartPos = msg.indexOf(_match, fromIdx) + _match.length();
  134.         int _nEndPos = msg.indexOf(_match2, fromIdx);
  135.         if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos)
  136.             return true;
  137.  
  138.         return false;
  139.     }
  140.  
  141.     /**
  142.      * 判断指定节点下的属性与值是否匹配
  143.      * 能够在多个相同节点不同属性、不同属性值得情况下,找到需要的节点
  144.      * 返回匹配的多个节点内容
  145.      * @param fieldName 指定的节点
  146.      * @param attribute 指定的属性
  147.      * @param value 指定的属性值
  148.      * @return 返回匹配的多个节点内容
  149.      */
  150.     public List<String> equalsAttribute(String fieldName, String attribute, String value) {
  151.         List<String> result = new ArrayList<String>();
  152.         int fromInx = 1;
  153.         String _retStr = "";
  154.         String _match = "<" + fieldName+ " ";
  155.         String _match2 = "</" + fieldName + ">";
  156.         int _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx);
  157.         int _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx);
  158.         while (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) {
  159.             _retStr = this.msg.substring(_nStartPos, _nEndPos);
  160.             int exist = _retStr.indexOf(attribute + "=\\"" + value + "\\"");
  161.             if (exist != -1) {
  162.                 result.add(_retStr);
  163.             }
  164.             fromInx++;
  165.             _nStartPos = StringUtils.ordinalIndexOf(msg, _match, fromInx);
  166.             _nEndPos = StringUtils.ordinalIndexOf(msg, _match2, fromInx);
  167.         }
  168.         return result;
  169.     }
  170.  
  171.     /**
  172.      * 从给定的字符串中获取指定节点的内容
  173.      * @param submsg 给定的字符串
  174.      * @param fieldName 指定的节点
  175.      * @return 指定节点的内容
  176.      */
  177.     public String decodeSubmsg(String submsg, String fieldName) {
  178.         String _retStr = "";
  179.         if (StringUtils.isNotBlank(submsg)) {
  180.             String _match = "<" + fieldName + ">";
  181.             String _match2 = "</" + fieldName + ">";
  182.             int _nStartPos = submsg.indexOf(_match) + _match.length();
  183.             int _nEndPos = submsg.indexOf(_match2);
  184.             if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) {
  185.                 _retStr = submsg.substring(_nStartPos, _nEndPos);
  186.             }
  187.         }
  188.         return _retStr;
  189.     }
  190.  
  191.     /**
  192.      * 在给定的的字符串中获取指定属性的值
  193.      * @param submsg    给定的字符串
  194.      * @param attribute 属性
  195.      * @return 返回属性值
  196.      */
  197.     public String decodeAttribute(String submsg, String attribute) {
  198.         String value = "";
  199.         if (StringUtils.isNotBlank(submsg)) {
  200.             String _match = attribute + "=\\"";
  201.            String _match2 = "\\"";
  202.             int _nStartPos = submsg.indexOf(_match) + _match.length();
  203.             int _nEndPos = submsg.indexOf(_match2, _nStartPos);
  204.             if (_nStartPos >= 0 && _nEndPos >= 0 && _nEndPos > _nStartPos) {
  205.                 value = submsg.substring(_nStartPos, _nEndPos);
  206.             }
  207.         }
  208.         return value;
  209.     }
  210.  
  211.     public static void main(String[] args) {
  212.         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>";
  213.         XmlPackParser parser = new XmlPackParser(testMsg);
  214.         String fieldName = "attachment";
  215.         String attribute = "type";
  216.         JobTasksData data = new JobTasksData();
  217.  
  218.         String upip = parser.decode("upip");
  219.         System.out.println("upip = " + upip);
  220.         System.out.println("//*****************************************//");
  221.         List<String> list = parser.equalsAttribute(fieldName, attribute, "config");
  222.         if (list.size() > 0) {
  223.             Iterator<String> it = list.iterator();
  224.             while(it.hasNext()) {
  225.                 String submsg = it.next();
  226.                 System.out.println(submsg);
  227.                 String value = parser.decodeAttribute(submsg, "defination");
  228.                 System.out.println("defination = " + value);
  229. //              String path = parser.decodeSubmsg(submsg, "path");
  230.                 String path = data.substringHttp(parser.decodeSubmsg(submsg, "path"),parser.decode("upip"));
  231.                 System.out.println("path = " + path);
  232.             }
  233.         }
  234.     }
  235.  
  236. }
  237.  
  238. //源代码片段来自云代码http://yuncode.net
  239.                        

回复 "java xml消息包解析"

这儿你可以回复上面这条便签

captcha