map转xml然后转string
private static final String PREFIX_XML = "";
private static final String SUFFIX_XML = "";
private static final String PREFIX_CDATA = "
private static final String SUFFIX_CDATA = "]]>";
public static String xmlFormat(Map parm,boolean isAddCDATA) {
StringBuffer strbuff = new StringBuffer(PREFIX_XML);
if (StringUtil.isNotEmpty(parm)) {
for (Entry entry : parm.entrySet()) {
strbuff.append("");
//这里是用CDATA标签包起来,原本以为body和sign需要包起来,但是发现不需要,又懒得删了,就改成了body1,sign1
//isNotNullOrEmptyStr是判断不为空的方法
if("attach".equalsIgnoreCase(entry.getKey()) ||"body1".equalsIgnoreCase(entry.getKey()) ||"sign1".equalsIgnoreCase(entry.getKey()) ){
strbuff.append(PREFIX_CDATA);
if (StringUtil.isNotNullOrEmptyStr(entry.getValue())) {
strbuff.append(entry.getValue());
}
strbuff.append(SUFFIX_CDATA);
}else{
if (StringUtil.isNotNullOrEmptyStr(entry.getValue())) {
strbuff.append(entry.getValue());
}
}
strbuff.append("").append(entry.getKey()).append(">");
}
}
return strbuff.append(SUFFIX_XML).toString();
}===============================================================================
请求api(注意:请求方式POST,设置编码格式UTF-8)public static String httpsRequest(String requestUrl,String requestMethod,String outputStr) {
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type","application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
// System.out.println("bf != null");
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
System.out.println("连接超时:{}"+ ce);
} catch (Exception e) {
System.out.println("https请求异常:{}"+ e);
}
return null;
}===============================================================================解析微信返回的xml格式数据,并根据业务需求,格式化后返回给客户端import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public static Map doXMLParse(String strxml) throws Exception {
strxml = strxml.replaceFirst("encoding=\".*\"","encoding=\"UTF-8\"");
if(null == strxml || "".equals(strxml)) {
return null;
}
Map m = new HashMap();
InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(in);
Element root = doc.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List children = e.getChildren();
if(children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
}
m.put(k,v);
}
in.close();
return m;
}
public static String getChildrenText(List children) {
StringBuffer sb = new StringBuffer();
if(!children.isEmpty()) {
Iterator it = children.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List list = e.getChildren();
sb.append("");
if(!list.isEmpty()) {
sb.append(getChildrenText(list));
}
sb.append(value);
sb.append("" + name + ">");
}
}
return sb.toString();
}