XML 和 JavaBean 的互转---org.dom4j.Document

前端之家收集整理的这篇文章主要介绍了XML 和 JavaBean 的互转---org.dom4j.Document前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

主要的包:dom4j-1.6.1.jar

一、将xml字符串转换为javaBean

1、xml字符串如下:

<WorkItemInfos>
  <WorkItem>
    <appellation>500605</appellation>
    <name>李萍</name>
    <ReceiveTime>2015-08-03 10:10:56</ReceiveTime>
    <FinishTime></FinishTime>
  </WorkItem>
  <WorkItem>
    <appellation>201021</appellation>
    <name>李晶晶</name>
    <ReceiveTime>2015-08-03 10:10:56</ReceiveTime>
    <FinishTime></FinishTime>
  </WorkItem>
</WorkItemInfos>


2、将xml字符串转换为Document文档对象:

	/**
	 * 根据字符串创建 document 对象
	 * @param xmlStr
	 * @return
	 */
	public static Document getDocumentByString(String xmlStr){
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xmlStr);
		} catch (DocumentException e) {
			logger.error("字符串转换成XML失败:" + e.getMessage());
		}
		return doc;
	}


3、将Document文档对象转换为javaBean:

	/**
	 * 
	 * @desc 解析xml
	 * @param xml串
	 * @throws Exception
	 * @date  2015年4月16日 下午5:13:03
	 */
	private List<FlowAuditDetail> parseXml(String xml) throws Exception{
		
		//方法返回的javaBean集合
		List<FlowAuditDetail> list = new ArrayList<FlowAuditDetail>();
		try {
			
			//根据字符串创建 document对象
			Document document = XMLUtil.getDocumentByString(xml);
			if(null == document){
				return null;
			}
			
			/*
			 * 下面这个循环,目的是解析Document对象
			 */
			//获取根元素
			Element rootElement = document.getRootElement();
			
			//获取根元素下面的元素集合[WorkItem]
			Iterator<Element> iter = rootElement.elementIterator("WorkItem");
			
			while (iter.hasNext()) {
				Element element = (Element) iter.next();
				
				//分别获取元素[appellation、name.....]的文本值
				String appellation = element.elementTextTrim("appellation");
				String name = element.elementTextTrim("name");
				String ReceiveTime = element.elementTextTrim("ReceiveTime");
				String FinishTime = element.elementTextTrim("FinishTime");
				
				//对javaBean[FlowAuditDetail]进行赋值
				FlowAuditDetail d = new FlowAuditDetail();
				d.setAppellation(appellation);
				d.setName(name);
				if(StringUtils.isNotNull(ReceiveTime)){
					System.out.println(ReceiveTime+"===========");
					Date date = sdf.parse(ReceiveTime);
					d.setReceivetime(date);
				}
				if(StringUtils.isNotNull(FinishTime)){
					Date date = sdf.parse(FinishTime);
					d.setFinishtime(date);
				}
				d.setCreatetime(new Date());
				list.add(d);
			}
		} catch (Exception e) {
			System.out.println("解析xml出错了!!");
			throw new Exception("解析xml出错了!!"+e.toString());
		}
		return list;
	}


二、将xml解析成Document对象的工具类

package com.openeap.common.utils;

import java.io.InputStream;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;

/**
 * dom4j 解析XML工具类
 * 
 */
public class XMLUtil {

	private static final Logger logger = Logger.getLogger(XMLUtil.class);

	/**
	 * 文件路径创建 document 对象
	 * 
	 * @param path
	 * @return
	 */
	public static Document getDocumentByPath(String path) {
		Document doc = null;
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(path);
		} catch (DocumentException e) {
			logger.error("读取XML失败:" + e.getMessage());
		}
		return doc;
	}
	/**
	 * 输入流创建 document 对象
	 * 
	 * @param path
	 * @return
	 */
	public static Document getDocumentByInputStream(InputStream in) {
		Document doc = null;
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(in);
		} catch (DocumentException e) {
			logger.error("读取XML失败:" + e.getMessage());
		}
		return doc;
	}
	
	/**
	 * classpath创建 document 对象
	 * 
	 * @param path	classpath
	 * @return
	 */
	public static Document getDocumentByClasspath(String path) {
		Document doc = null;
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));
		} catch (DocumentException e) {
			logger.error("读取XML失败:" + e.getMessage());
		}
		return doc;
	}
	
	/**
	 * 字符串创建 document 对象
	 * @param xmlStr
	 * @return
	 */
	public static Document getDocumentByString(String xmlStr){
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xmlStr);
		} catch (DocumentException e) {
			logger.error("字符串转换成XML失败:" + e.getMessage());
		}
		return doc;
	}
	

}

猜你在找的XML相关文章