发票xml下载,xml中文乱码问题修复
parent
fe44fcf1b8
commit
bb051d32db
@ -0,0 +1,92 @@
|
||||
package cn.iocoder.yudao.module.setting.tools;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ysd
|
||||
* @date 2021年08月03日 9:33
|
||||
*/
|
||||
public class JsonToXml {
|
||||
/**
|
||||
* Json to xml string.
|
||||
*
|
||||
* @param json the json
|
||||
* @return the string
|
||||
*/
|
||||
public static String jsonToXml(String json){
|
||||
try {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
||||
JSONObject jObj = JSON.parseObject(json);
|
||||
jsonToXmlstr(jObj,buffer);
|
||||
return buffer.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Json to xmlstr string.
|
||||
*
|
||||
* @param jObj the j obj
|
||||
* @param buffer the buffer
|
||||
* @return the string
|
||||
*/
|
||||
public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer) {
|
||||
Set<Map.Entry<String, Object>> se = jObj.entrySet();
|
||||
for (Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); ) {
|
||||
Map.Entry<String, Object> en = it.next();
|
||||
if (en.getValue() instanceof JSONObject) {
|
||||
buffer.append("<" + en.getKey() + ">");
|
||||
JSONObject jo = (JSONObject) en.getValue();
|
||||
jsonToXmlstr(jo, buffer);
|
||||
buffer.append("</" + en.getKey() + ">");
|
||||
} else if (en.getValue() instanceof JSONArray) {
|
||||
JSONArray jarray = (JSONArray) en.getValue();
|
||||
for (int i = 0; i < jarray.size(); i++) {
|
||||
buffer.append("<" + en.getKey() + ">");
|
||||
JSONObject jsonobject = jarray.getJSONObject(i);
|
||||
jsonToXmlstr(jsonobject, buffer);
|
||||
buffer.append("</" + en.getKey() + ">");
|
||||
}
|
||||
} else if (en.getValue().getClass().getName().equals("java.lang.String")) {
|
||||
buffer.append("<" + en.getKey() + ">" + encodeXmlString((String) en.getValue()));
|
||||
buffer.append("</" + en.getKey() + ">");
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static String encodeXmlString(String input) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c <= 0xD7FF || c >= 0xE000) {
|
||||
out.append(c);
|
||||
} else {
|
||||
out.append("&#x").append(Integer.toHexString(c)).append(";");
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The entry point of application.
|
||||
*
|
||||
* @param args the input arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
String xmlstr = jsonToXml("{\"class\": {\"student\": [{\"age\": \"18\",\"gender\": \"男\",\"name\": \"张三\",\"qk\": [{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"},{\"q1\": \"001\",\"q2\": \"002\",\"q3\": \"003\"}]},{\"age\": \"17\",\"gender\": \"男\",\"name\": \"李四\",\"qk\": {\"q1\": \"005\",\"q2\": \"006\",\"q3\": \"007\"}},{\"age\": \"19\",\"gender\": \"女\",\"name\": \"王五\",\"qk\": {\"q1\": \"008\",\"q2\": \"009\",\"q3\": \"010\"}}]}}");
|
||||
System.out.println(xmlstr);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue