更新日期20151012
package hrhx.dhm.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.thoughtworks.xstream.XStream;
/**
* @version 20150902
* @author duhongming
*
*/
@Component
public class XmlUtil {
private static XmlUtil instance = null;
private XStream xs = null;
private XmlUtil() {
this.xs = new XStream();
}
/**
* 初始化XmlUtil
* @return
*/
public static synchronized XmlUtil getInstance() {
if (instance == null) {
System.out.println("正在初始化XML处理对象.............");
instance = new XmlUtil();
}
return instance;
}
/**
* 将Object转化为XML字符串
* @param obj
* @return
* @throws Exception
*/
public String toXML(Object obj) throws Exception {
if (obj == null)
return null;
return this.xs.toXML(obj);
}
/**
* 对toXML方法重载,修改<节点名称>
* @param obj
* @param aliasMap
* @return
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public String toXML(Object obj,Map aliasMap) throws Exception {
if (obj == null)
return null;
for(Map.Entry entry:aliasMap.entrySet()){
this.xs.alias(entry.getKey(), entry.getValue());
}
return this.xs.toXML(obj);
}
/**
* 将Object转化为XML文件
* @param obj
* @param strFile
* @throws Exception
*/
public void toXMLFile(Object obj, String strFile) throws Exception {
if (obj == null)
return;
if ((strFile == null) || (strFile.equals("")))
throw new Exception("XML保存的文件名不能为空!");
String str = strFile;
str = str.replaceAll("//", "/");
File f = new File(str.substring( 0, str.lastIndexOf("/")));
if (!f.exists())
f.mkdirs();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(str));
this.xs.toXML(obj, fos);
fos.close();
} catch (Exception e) {
if (fos != null)
fos.close();
e.printStackTrace();
throw new Exception("将对象写到文件" + str + "失败!" + e.getMessage());
}
}
/**
* 将XML字符串转成Object
* @param xml
* @return
* @throws Exception
*/
public Object fromXML(String xml) throws Exception {
if ((xml == null) || (xml.equals("")))
return null;
return this.xs.fromXML(xml);
}
/**
* 将XML文件转成Object
* @param strFile
* @return
* @throws Exception
*/
public Object fromXMLFile(String strFile) throws Exception {
if ((strFile == null) || (strFile.equals("")))
return null;
FileInputStream fis = null;
Object obj = null;
String str = strFile;
str = str.replaceAll("//", "/");
File f = new File(str);
if (!f.exists())
return null;
try {
fis = new FileInputStream(f);
obj = this.xs.fromXML(fis).toString();
fis.close();
} catch (Exception e) {
if (fis != null)
fis.close();
e.printStackTrace();
throw new Exception("从文件" + str + "读取内容进行对象转换时失败!" + e.getMessage());
}
return obj;
}
public static void main(String[] args) throws Exception {
String begin = "2002-10-21 23:12:34";
begin = begin.substring( 5, 7) + "月" + begin.substring( 8, 10) + "日";
List list = new ArrayList();
list.add("duhongming");
list.add("24");
list.add(begin);
String listXml = getInstance().toXML(list);
System.out.println("将List数据转成XML:"+listXml);
System.out.println("将XML数据转成List:"+getInstance().fromXML(listXml));
String templateFileName = System.getProperty("user.dir")+"/target/demo.xml";
getInstance().toXMLFile(list, templateFileName);
System.out.println("将XML文件数据转成List:"+getInstance().fromXMLFile(templateFileName));
Map map = new HashMap();
map.put("name", "duhongming");
map.put("age", "24");
map.put("birthday", "7月26日");
String mapXml = getInstance().toXML(map);
System.out.println("将Map数据转成XML:"+mapXml);
System.out.println(("将XML数据转成Map:"+getInstance().fromXML(mapXml)));
}
}
附Java平台自带的注解功能:
package com.hrhx.db;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import com.sun.xml.internal.txw2.annotation.XmlElement;
@XmlRootElement
public class TestMenu {
private String menuName;
private String menuUrl;
private String userName;
private String timestamp;
@XmlElement
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
@XmlElement
public String getMenuUrl() {
return menuUrl;
}
public void setMenuUrl(String menuUrl) {
this.menuUrl = menuUrl;
}
@XmlElement
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@XmlElement
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public static void main(String[] args) {
JAXBContext jc;
try {
jc = JAXBContext.newInstance(TestMenu.class);
Marshaller ms = jc.createMarshaller();
TestMenu t = new TestMenu();
t.setMenuName("baidu");
t.setMenuUrl("http://www.baidu.com");
t.setTimestamp("2015-10-1");
t.setUserName("zhangsan");
// System.out.println(t);
ms.marshal(t, System.out);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。