工具类
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
*
* XML文档转换器。
*
* @param <T> 转换对象的类型
*/
public class XmlConverter<T> {
/** 日志记录对象。 */
private Log runLog = LogFactory.getLog(XmlConverter.class);
/** 实体类型 */
private Class<T> entityClass = null;
/**
* 构造函数。
* @param entityClass 实体类型
*/
public XmlConverter(Class<T> entityClass) {
this.entityClass = entityClass;
}
/**
* 创建没有表头的xmlString
* @param t 需要转换的对象
* @return
*/
public String createJaxbXml(T t) {
String xmlString = "";
if (t == null) {
runLog.info("createJaxbXml object is null.");
return xmlString;
}
try {
JAXBContext context = JAXBContext.newInstance(entityClass);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
// 去掉xml的头
//marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
// 格式化xml
//marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
// 把对象转化成xml字符串
StringWriter writer = new StringWriter();
marshaller.marshal(t,writer);
String xmlDocument = writer.toString().trim();
xmlString = xmlDocument;
runLog.info("createJaxbXml xmlString: " + xmlString);
} catch (JAXBException e) {
runLog.error("createJaxbXml failure.",e);
}
return xmlString;
}
/**
* 将XML文件解析并封装为实体集合。
*
* @param xmlContent
* XML文档内容
* @param root
* 是否去除根节点
* @return 解析后的实体集合
*/
public List<T> unmarshal(String xmlContent,boolean root) {
// 构建结果实体集合
List<T> resultList = new ArrayList<T>();
if (StringUtils.isBlank(xmlContent)) {
return resultList;
}
// 创建工厂对象,用户进行XML数据解析
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 解析XML数据,生成DOM对象
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(xmlContent);
InputSource is = new InputSource(sr);
Document document = builder.parse(is);
// 将根节点下的所有元素解析为对象
NodeList nodeList = null;
if (root) {
// 去除根节点元素
Element list = document.getDocumentElement();
nodeList = list.getChildNodes();
} else {
// 将根节点下的所有元素解析为对象
nodeList = document.getChildNodes();
}
int size = nodeList.getLength();
//add by chenya 0503
/*if (nodeList.getLength()== 0){
resultList.add(null);
}*///end chenya
for (int i = 0; i < size; i++) {
resultList.add(createEntity(nodeList.item(i)));
}
// 释放内存
is = null;
try {
sr.close();
} catch (Exception e) {
}
sr = null;
builder = null;
} catch (Exception e) {
runLog.error(e);
} finally {
factory = null;
}
return resultList;
}
/**
* 将XML节点转换为对象(该对象必须配置@XmlRootElement属性)。
*
* @param node
* XML节点
* @return 转换后的对象
*/
@SuppressWarnings({ "unchecked" })
public T unmarshal(String xml) {
if(StringUtils.isBlank(xml)) return null;
StringReader reader = null;
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(entityClass);
Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
reader = new StringReader(xml);
return (T) unmarshaller.unmarshal(reader);
} catch (JAXBException ex) {
runLog.error(ex);
}
return null;
}
/**
* 将xml文件转换为对象
* @param context 文件路径
* @return
*/
@SuppressWarnings("unchecked")
public T convertXmlString(String context) {
try {
JAXBContext jc = JAXBContext.newInstance(entityClass);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(new File(context));
} catch (JAXBException e) {
runLog.error(e);
}
return null;
}
/**
* 将xml文件转换为对象
* @param fileConf xml文件
* @return
*/
@SuppressWarnings("unchecked")
public T convertXmlFile(File fileConf) {
InputStream inputStrem = null;
try {
JAXBContext jc = JAXBContext.newInstance(entityClass);
Unmarshaller u = jc.createUnmarshaller();
inputStrem = new FileInputStream(fileConf);
return (T) u.unmarshal(inputStrem);
} catch (Exception e) {
runLog.error(e);
}
return null;
}
/**
* 将xml文件转换为对象
* @param fileConf xml文件
* @return
*/
@SuppressWarnings("unchecked")
public T convertXmlInputStream(String context) {
InputStream inputStrem = null;
try {
JAXBContext jc = JAXBContext.newInstance(entityClass);
Unmarshaller u = jc.createUnmarshaller();
inputStrem = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(context);
return (T) u.unmarshal(inputStrem);
} catch (Exception e) {
runLog.error(e);
}
return null;
}
public Class<T> getEntityClass() {
return entityClass;
}
public void setEntityClass(Class<T> entityClass) {
this.entityClass = entityClass;
}
/**
* 将数据实体装载为XML文档内容。
*
* @param entities
* 数据实体
* @return 装载后的XML文档内容
*/
public String marshal(List<T> entities) {
// 初始化返回值
String xmlContent = null;
// 入参判断
if (null == entities || 0 >= entities.size()) {
return xmlContent;
}
// 先将内存中的数据结构表示为DOM文档格式
// 然后再通过XSLT直接转换为XML字符流
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
ByteArrayOutputStream baos = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// 将数据实体转换为XML元素并加入到根节点中
for (T entity : entities) {
addEntityToElement(entity,document);
}
// 数据全部存入DOM树结构中后,使用JAXP提供的XSLT直接进行转换
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
// 设置目标XML文档的语言
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
// 转换源为存放输入信息的DOM数据结构
DOMSource source = new DOMSource(document);
// 构造转换目的流
baos = new ByteArrayOutputStream();
// 生成表示转换结果的数据结构,并设置转换目的流的字符编码
StreamResult result = new StreamResult(baos);
// 调用XSLT进行数据转换
transformer.transform(source,result);
// 将转换结果保存到xmlData属性中,注意对XML字符编码的转换
xmlContent = baos.toString("UTF-8");
// 清空引用
result = null;
source = null;
transformer = null;
tFactory = null;
builder = null;
} catch (Exception e) {
runLog.error(e);
} finally {
// 释放内存
factory = null;
if (null != baos) {
try {
baos.close();
} catch (Exception e) {
runLog.error(e);
}
}
}
return xmlContent;
}
/**
* 将对象转换为XML节点(需转换对象必须配置@XmlRootElement属性)。
*
* @param t
* 需要转换的对象
* @param parentNode
* 转换后节点的父节点
*/
private void addEntityToElement(T t,Node parentNode) {
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(entityClass);
Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.marshal(t,parentNode);
} catch (JAXBException ex) {
runLog.error(ex);
}
}
/**
* 将XML节点转换为对象(该对象必须配置@XmlRootElement属性)。
*
* @param node
* XML节点
* @return 转换后的对象
*/
@SuppressWarnings("unchecked")
private T createEntity(Node node) {
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(entityClass);
Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
return (T) unmarshaller.unmarshal(node);
} catch (JAXBException ex) {
runLog.error(ex);
}
return null;
}
}
xml 内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rsCis>
<ciInfo>
<id>267</id>
<typeName>路由器</typeName>
<itemList>
<pairItem>
<key>本年度综评</key>
<value></value>
</pairItem>
<pairItem>
<key>abc</key>
<value></value>
</pairItem>
<pairItem>
<key>维保开始时间</key>
<value>2017-04-20</value>
</pairItem>
<pairItem>
<key>审计结果</key>
<value>未审计</value>
</pairItem>
<pairItem>
<key>机房A_A配线间</key>
<value></value>
</pairItem>
<pairItem>
<key>登录用户名</key>
<value></value>
</pairItem>
<pairItem>
<key>SNMP共同体名</key>
<value></value>
</pairItem>
<pairItem>
<key>CI管理员</key>
<value></value>
</pairItem>
<pairItem>
<key>建筑物</key>
<value></value>
</pairItem>
<pairItem>
<key>维保结束时间</key>
<value>2020-04-20</value>
</pairItem>
<pairItem>
<key>机柜</key>
<value></value>
</pairItem>
<pairItem>
<key>槽位数</key>
<value></value>
</pairItem>
<pairItem>
<key>版本号</key>
<value></value>
</pairItem>
<pairItem>
<key>起始U位</key>
<value></value>
</pairItem>
<pairItem>
<key>资产编号</key>
<value></value>
</pairItem>
<pairItem>
<key>综评结论</key>
<value></value>
</pairItem>
<pairItem>
<key>投产时间</key>
<value></value>
</pairItem>
<pairItem>
<key>电源型号</key>
<value></value>
</pairItem>
<pairItem>
<key>管理IP</key>
<value>10.144.10.101</value>
</pairItem>
<pairItem>
<key>验收时间</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_number</key>
<value>1</value>
</pairItem>
<pairItem>
<key>管理员I_IAJ_J</key>
<value>张小小</value>
</pairItem>
<pairItem>
<key>ci_common_name</key>
<value>三楼路由器</value>
</pairItem>
<pairItem>
<key>所属环境</key>
<value></value>
</pairItem>
<pairItem>
<key>上架类型</key>
<value></value>
</pairItem>
<pairItem>
<key>设备登录协议</key>
<value></value>
</pairItem>
<pairItem>
<key>供应商</key>
<value></value>
</pairItem>
<pairItem>
<key>品牌</key>
<value>惠普</value>
</pairItem>
<pairItem>
<key>购置合同号</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_state</key>
<value>已部署</value>
</pairItem>
<pairItem>
<key>序列号</key>
<value></value>
</pairItem>
<pairItem>
<key>维保A_A服务商</key>
<value></value>
</pairItem>
<pairItem>
<key>SNMP版本</key>
<value></value>
</pairItem>
<pairItem>
<key>故障记录</key>
<value></value>
</pairItem>
<pairItem>
<key>结束U位</key>
<value></value>
</pairItem>
<pairItem>
<key>城市</key>
<value>北京</value>
</pairItem>
<pairItem>
<key>维保A_A服务合同号</key>
<value></value>
</pairItem>
<pairItem>
<key>其他因素</key>
<value></value>
</pairItem>
<pairItem>
<key>备注</key>
<value></value>
</pairItem>
<pairItem>
<key>变更记录</key>
<value></value>
</pairItem>
<pairItem>
<key>制造商名称</key>
<value></value>
</pairItem>
<pairItem>
<key>管理员I_IBJ_J</key>
<value></value>
</pairItem>
<pairItem>
<key>电源数量</key>
<value></value>
</pairItem>
<pairItem>
<key>电源功率I_IWJ_J</key>
<value></value>
</pairItem>
<pairItem>
<key>网络I_I功能J_J区域</key>
<value></value>
</pairItem>
<pairItem>
<key>型号</key>
<value>SR8808</value>
</pairItem>
<pairItem>
<key>购置时间</key>
<value></value>
</pairItem>
<pairItem>
<key>是否纳入监控</key>
<value></value>
</pairItem>
<pairItem>
<key>楼层</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_description</key>
<value></value>
</pairItem>
<pairItem>
<key>评价人</key>
<value></value>
</pairItem>
<pairItem>
<key>维护团队</key>
<value>网络</value>
</pairItem>
</itemList>
<name>三楼路由器</name>
<statistic_1>-1</statistic_1>
<statistic_2>-1</statistic_2>
<updateDate>2017-06-05T15:39:20+08:00</updateDate>
<createDate>2017-04-28T11:37:49+08:00</createDate>
<updateAccount>test</updateAccount>
<createAccount>admin</createAccount>
<commonName>三楼路由器</commonName>
<commonDesc></commonDesc>
<commonNumber>1</commonNumber>
<commonState>已部署</commonState>
<completeCiNo>LYQ-1</completeCiNo>
</ciInfo>
<ciInfo>
<id>408</id>
<typeName>路由器</typeName>
<itemList>
<pairItem>
<key>本年度综评</key>
<value></value>
</pairItem>
<pairItem>
<key>abc</key>
<value>哈哈美滋滋</value>
</pairItem>
<pairItem>
<key>维保开始时间</key>
<value></value>
</pairItem>
<pairItem>
<key>审计结果</key>
<value>未审计</value>
</pairItem>
<pairItem>
<key>机房A_A配线间</key>
<value></value>
</pairItem>
<pairItem>
<key>登录用户名</key>
<value></value>
</pairItem>
<pairItem>
<key>SNMP共同体名</key>
<value></value>
</pairItem>
<pairItem>
<key>CI管理员</key>
<value></value>
</pairItem>
<pairItem>
<key>建筑物</key>
<value></value>
</pairItem>
<pairItem>
<key>维保结束时间</key>
<value></value>
</pairItem>
<pairItem>
<key>机柜</key>
<value></value>
</pairItem>
<pairItem>
<key>槽位数</key>
<value></value>
</pairItem>
<pairItem>
<key>版本号</key>
<value></value>
</pairItem>
<pairItem>
<key>起始U位</key>
<value></value>
</pairItem>
<pairItem>
<key>资产编号</key>
<value></value>
</pairItem>
<pairItem>
<key>综评结论</key>
<value></value>
</pairItem>
<pairItem>
<key>投产时间</key>
<value></value>
</pairItem>
<pairItem>
<key>电源型号</key>
<value></value>
</pairItem>
<pairItem>
<key>管理IP</key>
<value>192.168.127.233</value>
</pairItem>
<pairItem>
<key>验收时间</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_number</key>
<value>2</value>
</pairItem>
<pairItem>
<key>管理员I_IAJ_J</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_name</key>
<value>rt2楼路由器</value>
</pairItem>
<pairItem>
<key>所属环境</key>
<value></value>
</pairItem>
<pairItem>
<key>上架类型</key>
<value></value>
</pairItem>
<pairItem>
<key>设备登录协议</key>
<value></value>
</pairItem>
<pairItem>
<key>供应商</key>
<value></value>
</pairItem>
<pairItem>
<key>品牌</key>
<value></value>
</pairItem>
<pairItem>
<key>购置合同号</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_state</key>
<value></value>
</pairItem>
<pairItem>
<key>序列号</key>
<value></value>
</pairItem>
<pairItem>
<key>维保A_A服务商</key>
<value></value>
</pairItem>
<pairItem>
<key>SNMP版本</key>
<value></value>
</pairItem>
<pairItem>
<key>故障记录</key>
<value></value>
</pairItem>
<pairItem>
<key>结束U位</key>
<value></value>
</pairItem>
<pairItem>
<key>城市</key>
<value></value>
</pairItem>
<pairItem>
<key>维保A_A服务合同号</key>
<value></value>
</pairItem>
<pairItem>
<key>其他因素</key>
<value></value>
</pairItem>
<pairItem>
<key>备注</key>
<value></value>
</pairItem>
<pairItem>
<key>变更记录</key>
<value></value>
</pairItem>
<pairItem>
<key>制造商名称</key>
<value></value>
</pairItem>
<pairItem>
<key>管理员I_IBJ_J</key>
<value></value>
</pairItem>
<pairItem>
<key>电源数量</key>
<value></value>
</pairItem>
<pairItem>
<key>电源功率I_IWJ_J</key>
<value></value>
</pairItem>
<pairItem>
<key>网络I_I功能J_J区域</key>
<value></value>
</pairItem>
<pairItem>
<key>型号</key>
<value></value>
</pairItem>
<pairItem>
<key>购置时间</key>
<value></value>
</pairItem>
<pairItem>
<key>是否纳入监控</key>
<value></value>
</pairItem>
<pairItem>
<key>楼层</key>
<value></value>
</pairItem>
<pairItem>
<key>ci_common_description</key>
<value></value>
</pairItem>
<pairItem>
<key>评价人</key>
<value></value>
</pairItem>
<pairItem>
<key>维护团队</key>
<value>网络</value>
</pairItem>
</itemList>
<name>rt2楼路由器</name>
<statistic_1>-1</statistic_1>
<statistic_2>-1</statistic_2>
<updateDate>2017-11-20T11:09:04+08:00</updateDate>
<createDate>2017-11-20T11:09:04+08:00</createDate>
<updateAccount>admin</updateAccount>
<createAccount>admin</createAccount>
<commonName>rt2楼路由器</commonName>
<commonNumber>2</commonNumber>
<completeCiNo>LYQ-2</completeCiNo>
</ciInfo>
</rsCis>
java bean
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
* @author zphea
* cmdb的设备列表
*
*/
@XmlRootElement(name="rsCis")
@XmlAccessorType(XmlAccessType.FIELD)
public class RsCis implements Serializable{
/**
*
*/
@XmlTransient
private static final long serialVersionUID = 7972824696347988460L;
@XmlElements({
@XmlElement(name="ciInfo",type=CiInfo.class)
})
private List<CiInfo> list;
public List<CiInfo> getList() {
return list;
}
public void setList(List<CiInfo> list) {
this.list = list;
}
}
-------
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
* @author zphea
* cmdb的设备信息
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ciInfo")
public class CiInfo implements Serializable{
/**
*
*/
@XmlTransient
private static final long serialVersionUID = -6749007852081069327L;
@XmlElement
private String id;//
@XmlElement
private String typeName;
@XmlElement
private String name;
@XmlElement
private String statistic_1;
@XmlElement
private String statistic_2;
@XmlElement
private String updateDate;
@XmlElement
private String createDate;
@XmlElement
private String updateAccount;
@XmlElement
private String createAccount;
@XmlElement
private String commonName;
@XmlElement
private String commonDesc;
@XmlElement
private String commonNumber;
@XmlElement
private String commonState;
@XmlElement
private String completeCiNo;
@XmlElementWrapper(name="itemList")
@XmlElement(name="pairItem")
private List<PairItem> itemList;
public String getId() {
return id;
}
public List<PairItem> getItemList() {
return itemList;
}
public void setItemList(List<PairItem> itemList) {
this.itemList = itemList;
}
public void setId(String id) {
this.id = id;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatistic_1() {
return statistic_1;
}
public void setStatistic_1(String statistic_1) {
this.statistic_1 = statistic_1;
}
public String getStatistic_2() {
return statistic_2;
}
public void setStatistic_2(String statistic_2) {
this.statistic_2 = statistic_2;
}
public String getUpdateDate() {
return updateDate;
}
public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getUpdateAccount() {
return updateAccount;
}
public void setUpdateAccount(String updateAccount) {
this.updateAccount = updateAccount;
}
public String getCreateAccount() {
return createAccount;
}
public void setCreateAccount(String createAccount) {
this.createAccount = createAccount;
}
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
public String getCommonDesc() {
return commonDesc;
}
public void setCommonDesc(String commonDesc) {
this.commonDesc = commonDesc;
}
public String getCommonNumber() {
return commonNumber;
}
public void setCommonNumber(String commonNumber) {
this.commonNumber = commonNumber;
}
public String getCommonState() {
return commonState;
}
public void setCommonState(String commonState) {
this.commonState = commonState;
}
public String getCompleteCiNo() {
return completeCiNo;
}
public void setCompleteCiNo(String completeCiNo) {
this.completeCiNo = completeCiNo;
}
-------------转换方法 xmlContent 为xml文档内容
XmlConverter<RsCis> entity = new XmlConverter<RsCis>(RsCis.class);
rsCis = entity.unmarshal(xmlContent);
注意在 集合的 属性字段上加注解。
例如
@XmlElementWrapper(name="itemList") @XmlElement(name="pairItem") private List<PairItem> itemList;
原文链接:/xml/293618.html