Exemplo n.º 1
0
  /**
   * 将RyJbxx转换为xml格式的字符串
   *
   * @param head XML的头名字<head>..</head>
   * @return 返回XML字符串
   * @throws Exception
   */
  public static String toXml(String head, ConsructionHeadVO headVo, Consruction1104RequestVO rj)
      throws Exception {

    StringBuffer sb = new StringBuffer();
    Field[] field = rj.getClass().getDeclaredFields();
    String ENDL = "\n";
    String TABLE = "\t";
    String name = "";

    for (int i = 0; i < field.length; i++) {
      // 跳过表态属性
      String mod = Modifier.toString(field[i].getModifiers());
      if (mod.indexOf("static") != -1) {
        continue;
      }
      name = TEST.FirstStringToUpperCase(field[i].getName());
      Method m = rj.getClass().getMethod("get" + name);
      String value = (String) m.invoke(rj);
      // 构造XML
      sb.append(TABLE)
          .append("<")
          .append(name)
          .append(">")
          .append(value)
          .append("</")
          .append(name)
          .append(">")
          .append(ENDL);
    }
    return "<" + head + ">" + sb.toString() + "</" + head + ">";
  }
Exemplo n.º 2
0
  /**
   * 将VO转换为xml格式的字符串
   *
   * @param strVo
   * @return 返回XML字符串
   * @throws Exception
   */
  public static String VoToXml(Object objVo) throws Exception {

    StringBuffer sb = new StringBuffer();
    Field[] field = objVo.getClass().getDeclaredFields();
    String ENDL = "\n";
    String TABLE = "\t";
    String name = "";

    for (int i = 0; i < field.length; i++) {
      // 跳过表态属性
      String mod = Modifier.toString(field[i].getModifiers());
      if (mod.indexOf("static") != -1) {
        continue;
      }
      name = TEST.FirstStringToUpperCase(field[i].getName());
      Method m = objVo.getClass().getMethod("get" + name);
      String value = (String) m.invoke(objVo);

      // 当value是null的时候,变成“”
      if (StringUtil.isBlank(value)) {
        value = "";
      }

      // 构造XML
      sb.append(TABLE)
          .append("<")
          .append(name)
          .append(">")
          .append(value)
          .append("</")
          .append(name)
          .append(">")
          .append(ENDL);
    }
    return sb.toString();
  }