新建一个XML工具类定义自己需要的拼接信息
public class XmlUtil {
public static Stringlt ="<";
public static StringltEnd ="";
public static Stringrt =">";
public static StringrhtEnd ="/>";
public static Stringquotes ="\"";
public static Stringequal ="=";
public static Stringblank =" ";
/**
* @param xmlVo
* @return
* @category 拼接XML各元素信息
*/
public static StringBuffer elementToXml(XMLVo xmlVo) {
StringBuffer result =new StringBuffer();
// 元素开始
result.append(lt).append(xmlVo.getName());
// 判断是否有属性
if (xmlVo.getProperty() !=null && xmlVo.getProperty().size() >0) {
Iterator iter = xmlVo.getProperty().keySet().iterator();
while (iter.hasNext()) {
String key = String.valueOf(iter.next());
String value = xmlVo.getProperty().get(key);
result.append(blank).append(key).append(equal).append(quotes)
.append(value).append(quotes).append(blank);
}
}
result.append(rt);// 结束标记
/*
* 判断是否是叶子节点 是叶子节点,添加节点内容 不是叶子节点,循环添加子节点
*/
if (xmlVo.isIsleaf()) {
result.append(xmlVo.getNodeText());
}else {
for (XMLVo temp : xmlVo.getChild()) {
result.append(elementToXml(temp));
}
}
// 元素结束
result.append(ltEnd).append(xmlVo.getName()).append(rt);
return result;
}
/**
* @param xmlVo
* @return
* @category 拼接XML申明信息
*/
public static String element2Xml(XMLVo xmlVo) {
StringBuffer body =elementToXml(xmlVo);
StringBuffer head =new StringBuffer(
"");
head.append(body);
return head.toString();
}
}
新建一个XMLVo对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class XMLVo {
private Stringname;// 元素名
private StringnodeText ="";// 文本值
private Mapproperty =new HashMap();// 属性
private boolean isleaf =true;// 是否子节点
private Listchild =new ArrayList();// 子节点
public XMLVo(String name) {
this.name = name;
}
/**
* 添加属性
*
* @param key
* @param value
*/
public void addProperty(String key, String value) {
this.property.put(key, value);
}
/**
* 添加子节点
*
* @param xl
*/
public void addChild(XMLVo xl) {
this.child.add(xl);
if (this.isleaf &&this.child.size() >0) {
this.isleaf =false;
}
}
}
public String StringToXML(String CustomerName, String CertType, String CertID) {
log.info("CustomerName:{}-->,CustomerName:{}-->,CustomerName:{}-->", CustomerName, CertType, CertID);
XMLVo amarRuleService =new XMLVo("AmarRuleService");
amarRuleService.addProperty("TradeCode",TradeCode);
XMLVo callNo =new XMLVo("CallNo");
String CallNo = IDUtil.getUUId();
callNo.setNodeText(CallNo);
XMLVo customerName =new XMLVo("CustomerName");
customerName.setNodeText(CustomerName);
XMLVo certType =new XMLVo("CertType");
certType.setNodeText(CertType);
XMLVo certID =new XMLVo("CertID");
certID.setNodeText(CertID);
XMLVo serialNo =new XMLVo("SerialNo");
serialNo.setNodeText("");
XMLVo relationShip1 =new XMLVo("RelationShip1");
relationShip1.setNodeText("");
XMLVo callUserID =new XMLVo("CallUserID");
callUserID.setNodeText(CallUserID);
XMLVo typeNo =new XMLVo("TypeNo");
typeNo.setNodeText("");
XMLVo attrib2 =new XMLVo("Attrib2");
attrib2.setNodeText(Attrib2);
XMLVo timeLimit =new XMLVo("TimeLimit");
timeLimit.setNodeText(TimeLimit);
amarRuleService.addChild(callNo);
amarRuleService.addChild(customerName);
amarRuleService.addChild(certType);
amarRuleService.addChild(certID);
amarRuleService.addChild(serialNo);
amarRuleService.addChild(relationShip1);
amarRuleService.addChild(callUserID);
amarRuleService.addChild(typeNo);
amarRuleService.addChild(attrib2);
amarRuleService.addChild(timeLimit);
log.info("XmlUtil.element2Xml(axmlVo){}", XmlUtil.element2Xml(amarRuleService));
return XmlUtil.element2Xml(amarRuleService);
}