使用WebService传递XML格式的字符串作为入参时的工具类

在使用webservice的时候,我们有时候需要使用XML格式来进行传递入参或者是出参,那么入参格式怎么来进行拼接呢?
请看

/**
 * @Author: yld
 * @Date: 2018-12-03 10:36
 * @Version 1.0
 */
public class XmlUtils {
    public static String lt = "<";
    public static String ltEnd = "</";
    public static String rt = ">";
    public static String rhtEnd = "/>";
    public static String quotes = "\"";
    public static String equal = "=";
    public static String blank = " ";

    /**
     * @category 拼接xml个元素信息
     * @param element
     * @return
     */
    public static StringBuffer elementToXml(Element element){
        StringBuffer result = new StringBuffer();
        //元素开始
        result.append(lt).append(element.getName());
        //判断是否有属性
        if (element.getProperty() != null && element.getProperty().size() > 0){
            Iterator<String> iterator = element.getProperty().keySet().iterator();
            while (iterator.hasNext()){
                String key = String.valueOf(iterator.next());
                String value = element.getProperty().get(key);
                result.append(blank).append(key).append(equal).append(quotes).append(value).append(quotes).append(blank);
            }
        }
        //结束的标记
        result.append(rt);
        /**
         * 判断是否是叶子节点,如果是叶子节点,需要添加节点内容,不是叶子节点,那么循环添加子节点
         */
        if (element.isIsleaf()){
            result.append(element.getNodeText());
        }else {
            for (Element temp : element.getChild()) {
                result.append(elementToXml(temp));
            }
        }
        //元素结束
        result.append(ltEnd).append(element.getName()).append(rt);
        return result;
    }

    /**
     * 拼接xml申明信息
     * @param element
     * @return
     */
    public static String element2XML(Element element){
        StringBuffer body = elementToXml(element);
        StringBuffer head = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        head.append(body);
        return head.toString();
    }
}

接下来给你个使用案例来


			Element hosdata = new Element("hosdata");
			Element body = new Element("body");
			
			Element cardNo = new Element("cardNo");
			cardNo.setNodeText("WX180001");
			Element name = new Element("name");
			name.setNodeText("张三");
			Element sex = new Element("sex");
			sex.setNodeText("男");
			Element birthDate = new Element("birthDate");
			birthDate.setNodeText("1980-08-08");
			Element idNo = new Element("idNo");
			idNo.setNodeText("35072419800808xxxx");
			Element phone = new Element("phone");
			phone.setNodeText("13800138000");
			Element address = new Element("address");
			address.setNodeText("河北省承德市滦平县");
			body.addChild(cardNo);
			body.addChild(name);
			body.addChild(sex);
			body.addChild(birthDate);
			body.addChild(idNo);
			body.addChild(phone);
			body.addChild(address);
			hosdata.addChild(body);
			String result = XmlUtils.element2XML(hosdata);

打印出来的xml格式就是这个样子滴

<?xml version="1.0" encoding="utf-8"?>

<hosdata>
  <body>
    <cardNo>WX180001</cardNo>
    <name>张三</name>
    <sex>男</sex>
    <birthDate>1980-08-08</birthDate>
    <idNo>35072419800808xxxx</idNo>
    <phone>13800138000</phone>
    <address>xxxxxx</address>
  </body>
</hosdata>

打印出来的字符串,看起来很长,实际上转换出来没有那么的多,

猜你喜欢

转载自blog.csdn.net/yinlidong77/article/details/85002171