最近工作中调用接口,返回报文是String,取值不方便,需要转换为实体,回来自己简单写了个demo,基本上可以满足工作需求。
除了下面代码外,还要创建对应的实体。
package yh.test.t1113;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import java.util.Date;/** * Created by vincce on 2015/11/13. */public class Test1113 { Logger logger = LoggerFactory.getLogger(Test1113.class); public static User user(){ User user = new User(); user.setUserId("1"); user.setUserName("jack"); user.setUserAge("21"); user.setUserSex("male"); user.setEmail("3598649001@qq.com"); user.setCreateTime(new Date()); return user; } public static void main(String[] args) { /**entity to xml*/// xmlToEntity(user()); /** xml to entity*/ entityToXML(readLocalText()); /**read local file*/// readLocalText(); } /** * 转换器util * @param user * @return */ public static XStream ConvertUtil(User user) { System.out.println("------------开始调用转换器 ConvertUtil------------"); XStream xStream = new XStream(new DomDriver("GBK")); xStream.alias("user", User.class); xStream.aliasField("userId", User.class, "userId"); xStream.aliasField("userName", User.class, "userName"); xStream.aliasField("userSex", User.class, "userSex"); xStream.aliasField("userAge", User.class, "userAge"); xStream.aliasField("email", User.class, "email"); xStream.aliasField("createTime", User.class, "createTime"); return xStream; } /** * entity to xml * @return */ public static String xmlToEntity(User user) { String xml = ConvertUtil(user).toXML(user); System.out.println("---------开始输出xml---------"); System.out.println(xml); System.out.println("---------输出xml结束---------"); return xml; } /** * xml to entity * @param strXML 字符串 * @return */ public static User entityToXML(String strXML){ User user = (User)ConvertUtil(null).fromXML(strXML,new User()); System.out.println("------------开始转换为实体------------"); System.out.println(user.toString()); System.out.println(user.getUserAge()); System.out.println("------------实体转换结束------------"); return user; } /** * 读取本地 xml 文件 * @return */ public static String readLocalText(){ StringBuffer stringBuffer = new StringBuffer(); String str = null; String filePath="E:\\study\\test\\userXML.txt"; System.out.println("------------开始读取本地xml文件------------文件路径"+filePath); File file = new File(filePath); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; if(file.exists() && file.isFile()){ try { inputStreamReader = new InputStreamReader(new FileInputStream(file)); bufferedReader = new BufferedReader(inputStreamReader); while ((str = bufferedReader.readLine()) != null){ stringBuffer.append(str); } System.out.println("------------开始输出stringBuffer------------"); System.out.println(stringBuffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(inputStreamReader != null){ try { inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }else { System.out.println("指定目录下的文件找不到!"); } System.out.println("------------本地文件输出成功------------"); return stringBuffer.toString(); }}