net.unmz.java.util.xml.XmlUtils Maven / Gradle / Ivy
/**
* Project Name:pay-protocol
* File Name:Xml.java
* Package Name:cn.swiftpass.pay.protocol
* Date:2014-8-10下午10:48:21
*/
package net.unmz.java.util.xml;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import net.unmz.java.util.json.JsonUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
/**
* 功能描述:XML的工具方法
*
* @author [email protected]
* @version 1.0
* @since JDK 1.8
*
*/
public class XmlUtils {
/**
* request转字符串
* @param request
* @return
*/
public static String parseRequst(HttpServletRequest request) {
String body = "";
try {
ServletInputStream inputStream = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while (true) {
String info = br.readLine();
if (info == null) {
break;
}
if (body == null || "".equals(body)) {
body = info;
} else {
body += info;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
}
public static String parseXML(SortedMap parameters) {
StringBuffer sb = new StringBuffer();
sb.append("");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (null != v && !"".equals(v) && !"appkey".equals(k)) {
sb.append("<" + k + ">" + parameters.get(k) + "" + k + ">\n");
}
}
sb.append(" ");
return sb.toString();
}
/**
* 从request中获得参数Map,并返回可读的Map
*
* @param request
* @return
*/
public static SortedMap getParameterMap(HttpServletRequest request) {
// 参数Map
Map properties = request.getParameterMap();
// 返回值Map
SortedMap returnMap = new TreeMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if (null == valueObj) {
value = "";
} else if (valueObj instanceof String[]) {
String[] values = (String[]) valueObj;
for (int i = 0; i < values.length; i++) {
value = values[i] + ",";
}
value = value.substring(0, value.length() - 1);
} else {
value = valueObj.toString();
}
returnMap.put(name, value.trim());
}
return returnMap;
}
/**
* 转XMLmap
*
* @param xmlBytes
* @param charset
* @return
* @throws Exception
* @author
*/
public static Map toMap(byte[] xmlBytes, String charset) throws Exception {
SAXReader reader = new SAXReader(false);
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
source.setEncoding(charset);
Document doc = reader.read(source);
Map params = XmlUtils.toMap(doc.getRootElement());
return params;
}
/**
* 转MAP
*
* @param element
* @return
* @author
*/
public static Map toMap(Element element) {
Map rest = new HashMap();
List els = element.elements();
for (Element el : els) {
rest.put(el.getName().toLowerCase(), el.getTextTrim());
}
return rest;
}
public static String toXml(Map params) {
StringBuilder buf = new StringBuilder();
List keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
buf.append("");
for (String key : keys) {
buf.append("<").append(key).append(">");
buf.append("");
buf.append("").append(key).append(">\n");
}
buf.append(" ");
return buf.toString();
}
/**
* 将传入xml文本转换成Java对象
* @Title: toBean
* @Description: TODO
* @param xmlStr
* @param cls xml对应的class类
* @return T xml对应的class类的实例对象
*
* 调用的方法实例:PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class);
*/
public static T toBean(String xmlStr,Class cls){
//注意:不是new Xstream(); 否则报错:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
XStream xstream= new XStream(new DomDriver());
xstream.processAnnotations(cls);
T obj=(T)xstream.fromXML(xmlStr);
return obj;
}
public static Element readerXml(String body, String encode) throws DocumentException, SAXException {
SAXReader reader = new SAXReader(false);
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
InputSource source = new InputSource(new StringReader(body));
source.setEncoding(encode);
Document doc = reader.read(source);
Element element = doc.getRootElement();
return element;
}
/**
* 将字符串XML内容解析并转换成字符串
* @param body Xml内容
* @param encode 编码
* @return
* @throws DocumentException
*/
public static String toString(String body,String encode) throws DocumentException, SAXException {
Element element = readerXml(body, encode);
Map map = toMap(element);
return JsonUtils.MapToJSON(map);
}
}