示例#1
0
  /**
   * 将java对象转换成xml格式数据
   *
   * @param root
   * @param data
   */
  public void nestParse(Element root, Map<?, ?> data) {
    Map<?, ?> dataMap = (Map<?, ?>) data;
    String key = "";
    Iterator<?> it = dataMap.entrySet().iterator();

    while (it.hasNext()) {
      Entry<?, ?> entry = (Entry<?, ?>) it.next();
      key = (String) entry.getKey();
      Object value = entry.getValue();

      if (value instanceof Map) {
        // 嵌套元素
        Element element = root.addElement(key);
        nestParse(element, (Map<?, ?>) value);
      } else if (value instanceof List) {
        // repeate元素
        List<?> valList = (List<?>) value;
        for (int i = 0; i < valList.size(); i++) {
          Element element = root.addElement(key);
          if (valList.get(i) instanceof String) {
            element.setText(valList.get(i).toString());
          } else if (valList.get(i) instanceof Map) {
            nestParse(element, (Map<?, ?>) valList.get(i));
          }
        }
      } else {
        Element element = root.addElement(key);
        if (!StringUtil.isNullOrEmpty(value)) {
          element.setText(value.toString());
        } else {
          element.setText("");
        }
      }
    }
  }
示例#2
0
文件: SoapUtil.java 项目: a4m/basic
  /**
   * 功能描述:封装soap报文信息
   *
   * @return 返回值
   * @throw 异常描述
   * @see 需要参见的其它内容
   */
  public static String mergeSoapMessage(
      Map<String, Object> nodeHash, Map<String, Object> paramHash) {
    String soapMsg = "";
    if (StringUtil.isNullOrEmpty(nodeHash) || StringUtil.isNullOrEmpty(paramHash)) {
      return soapMsg;
    }

    if (!nodeHash.containsKey("operationName")) {
      return soapMsg;
    }
    if (!nodeHash.containsKey("namespace")) {
      return soapMsg;
    }
    if (!nodeHash.containsKey("serviceCode")) {
      return soapMsg;
    }

    // 操作名称
    String operationName = nodeHash.get("operationName").toString();
    // 命名空间
    String namespace = nodeHash.get("namespace").toString();
    // Esb提供交易码
    String serviceCode = nodeHash.get("serviceCode").toString();

    Document doc = DocumentHelper.createDocument();
    if (null != doc) {
      Element root =
          doc.addElement("SOAP-ENV:Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
      root.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
      root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
      root.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

      Element body = root.addElement("SOAP-ENV:Body");
      Element operation = body.addElement("m:" + operationName, namespace);
      Element input = operation.addElement("input1");
      Element head = input.addElement("MbfHeader");
      head.addElement("MBServiceCode").addText(serviceCode);
      Element pbody = input.addElement("MbfBody");
      pbody.setText("test");

      XMLWriter xmlWriter = null;
      StringWriter writer = new StringWriter();
      xmlWriter = new XMLWriter(writer);
      try {
        xmlWriter.write(doc);
        parseObject2Xml.parser(paramHash);
        String replaceStr =
            parseObject2Xml.getMsg()
                + "</MbfBody></input1></m:"
                + operationName
                + "></SOAP-ENV:Body></SOAP-ENV:Envelope>";
        soapMsg =
            writer
                .toString()
                .replace(
                    "test</MbfBody></input1></m:"
                        + operationName
                        + "></SOAP-ENV:Body></SOAP-ENV:Envelope>",
                    replaceStr.substring(38, replaceStr.length()));

      } catch (IOException e) {
        logger.error("io handler  occur error" + e.getMessage());
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (DocumentException e) {
        logger.debug("parse  object to xml occur error" + e.getMessage());
      }

      return soapMsg;
    }
    return soapMsg;
  }