public class StringToXml {
public static String getRequestMsgContent(String[] header, String[] name, String[] value) throws Exception {
StringBuffer sb = new StringBuffer("<message>");
sb.append("<function name=\"");
sb.append(header[0]);
sb.append("\">");
sb.append(header[1]);
sb.append("</function>");
sb.append("<service>");
sb.append(header[2]);
sb.append("</service>");
sb.append("<content>");
for (int i = 0; i < name.length; i++) {
if (value[i] != null && !"".equals(value[i])) {
sb.append("<");
sb.append(name[i]);
sb.append(">");
sb.append(value[i]);
sb.append("</");
sb.append(name[i]);
sb.append(">");
}
}
sb.append("</content>");
sb.append("</message>");
return sb.toString();
}
public static void main(String[] args) {
String[] elementName = {"operator_id", "phone_number","standard_type","area_type","begin_time", "end_time","calling_direction", "policy_id", "top_id", "bottom_id"};
String[] elementValue = {"6", "", "", "0", "", "", null, "", "1", "10"};
String[] queryCall ={"query", "query_bad_call_number_110", "call"};
try {
String s = getRequestMsgContent(queryCall, elementName, elementValue);
System.out.println("===========获取的xml========");
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
输入结果: