aade9c0585036591a55dd0bab355a4b2d7ec1538.svn-base
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.thinkgem.jeesite.common.utils.xml;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.thinkgem.jeesite.common.config.Global;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class JXmlUtils {
public static final String messageBStr = "<Message>\n";
public static final String dataBStr = "<Data>\n";
public static final String dataEStr = "\n</Data>";
public static final String messageEStr = "\n</Message>";
/***
* @Title: strChangeXML
* @Description: 调用生成XML报文文件的函数,参数为生成文件名和要生成的内容字符串
* @param @param filename
* @param @param str
* @param @throws IOException 设定文件
* @return void 返回类型
* @throws
*/
public static void strChangeXML(String filename, String str) throws IOException {
SAXReader saxReader = new SAXReader();
Document document;
File file =new File(Global.getUserfilesBaseDir() + Global.UP_DATA_URL);
if(!file.exists() && !file.isDirectory()){
file.mkdir();
}
String filenameStr = Global.getUserfilesBaseDir() + Global.UP_DATA_URL+"/"+filename+".xml";
try {
document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormat outFmt = OutputFormat.createPrettyPrint();
outFmt.setNewLineAfterDeclaration(false);
outFmt.setIndent(true);
outFmt.setIndent(" ");
outFmt.setEncoding("UTF-8");
XMLWriter output = new XMLWriter(new FileOutputStream(filenameStr), outFmt);
output.write(document);
output.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 方法名:XML2Bean
* 描述:XML转换为对象
* 作者:倪军军
* 日期:2015-6-23 下午12:59:51
* @param input
* @param clazz
* @param alias
* @return
* @throws IOException
* @return T
*/
@SuppressWarnings("unchecked")
public static <T> T XML2Bean(ByteArrayInputStream input, Class<T> clazz, Map<String,Class> alias) throws IOException {
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(clazz);
if(alias != null){
for(Map.Entry<String, Class> entry:alias.entrySet()){
xstream.alias(entry.getKey(),entry.getValue());
}
}
return (T) xstream.fromXML(input);
}
/**
* 方法名:getResponse
* 描述:获取响应XML
* 作者:倪军军
* 日期:2015-6-23 上午11:49:49
* @param body
* @param code
* @param msg
* @return
* @return String
*/
public static String getResponse(Object body,String code,String msg){
// 对象序列化
XStream xstream = new XStream(new DomDriver("UTF-8"));
xstream.autodetectAnnotations(true);
// xstream.setMode(XStream.NO_REFERENCES);
/*
Response response = new Response();
response.setMsg_code(code);
response.setMsg(msg);
response.setBody(body);
return xstream.toXML(response);
*/
return "";
}
}