最近有个项目,使用<xml>格式数据去请求和解析数据,特意在写出,有问题请指教;
public class XmlUtils {
/*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(Map<String, String> parameters) {
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
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("</xml>");
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
* @author
* @param xmlBytes
* @param charset
* @return
* @throws Exception
*/
public static Map<String, String> toMap(byte[] xmlBytes,String charset) throws Exception{
SAXReader reader = new SAXReader(false);
InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
source.setEncoding(charset);
Document doc = reader.read(source);
Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
return params;
}
/**
* 转MAP
* @author
* @param element
* @return
*/
public static Map<String, String> toMap(Element element){
Map<String, String> rest = new HashMap<String, String>();
List<Element> els = element.elements();
for(Element el : els){
rest.put(el.getName().toLowerCase(), el.getTextTrim());
}
return rest;
}
public static String toXml(Map<String, String> params){
StringBuilder buf = new StringBuilder();
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
buf.append("<xml>");
for(String key : keys){
buf.append("<").append(key).append(">");
buf.append("<![CDATA[").append(params.get(key)).append("]]>");
buf.append("</").append(key).append(">\n");
}
buf.append("</xml>");
return buf.toString();
}
}
public class XmlParseUtil {
public static byte[] getElementValue(byte[] termInfo, String element) {
if (element == null || element.length() == 0) {
return null;
}
// 签名为root及长度大于100时返回null
if (element.equals("root") || element.length() > XML_NAME_MAX) {
return null;
}
return getElements(termInfo).get(element);
}
public static HashMap<String, byte[]> getElements(byte[] termInfo) {
if (termInfo == null || termInfo.length < 1) {
return null;
}
String orig = new String(termInfo);
int index_start = orig.indexOf("<root>");
int index_end = orig.indexOf("</root>");
String info = orig.substring(index_start + 6, index_end);
HashMap<String, byte[]> map = new HashMap<String, byte[]>();
for (int i = 0; i < info.length();) {
String parseinfo = info.substring(i);
int index = parse(parseinfo, map);
if (index < 0) {
break;
}
i += index;
}
return map;
}
private static final int XML_NAME_MAX = 100;
private static int parse(String info, HashMap<String, byte[]> map) {
if (info.charAt(0) != '<') {
return -1;
}
int index = info.indexOf(">");
String tagName = info.substring(1, index);
if (tagName.length() > XML_NAME_MAX) {
return -1;
}
String elementValue = getElementValue(info.substring(tagName.length() + 2), tagName);
if (elementValue != null) {
map.put(tagName, elementValue.getBytes());
return info.indexOf(elementValue) + elementValue.length() + tagName.length() + 3;
}
return -1;
}
private static String getElementValue(String info, String tagName) {
int index = info.indexOf(tagName);
if (index < 2) {
return null;
}
if (index + tagName.length() + 1 > info.length()) {
return null;
}
String[] tags = info.split(tagName);
if (tags.length < 1) {
return null;
}
for (int i = 0; i < tags.length - 1; i++) {
String tag = tags[i];
if (tag.length() < 2) {
continue;
}
if (tag.substring(tag.length() - 2, tag.length()).equals("</")) {
if (tags[i + 1].charAt(0) == '>') {
return tag.substring(0, tag.length() - 2);
}
}
}
return null;
}
}
如有问题请回复,已亲测通过。
注:如有问题可以回复,看到第一时间分析解决,码农不易,感觉对您有用,帮助到您,可否打赏一杯可乐,在此谢过诸位,愿诸君终成大神,前程似锦~~~