复杂XML与复杂java对象的相互转换

本文探讨如何在Java中实现复杂XML结构与相应Java对象之间的高效转换。内容包括XML解析技术,如DOM、SAX和StAX,以及使用JAXB和XMLBeans等库进行对象绑定的方法。此外,还将讲解手动映射XML到Java对象的策略,以及在大型项目中遇到的挑战和最佳实践。

/**
 * Title: XMLUtil.java
 * Description: 
 * @author baymax
 * @time 创建时间:2018年1月9日 下午4:18:13
 * 
 * 
 * public class XMLUtil{ }
 * package com.paladin.business.GBLM;
 */


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;


import com.paladin.business.GBLM.bean.GBLMUtils;
import com.paladin.business.GBLM.bean.Host;
import com.paladin.business.GBLM.bean.Interface;
import com.paladin.business.GBLM.bean.Item;
import com.paladin.framework.util.Result;


/**
 * Title: XMLUtil.java
 * Description: 
 * @author baymax
 * @time 创建时间:2018年1月6日 下午5:03:06
 * 
 * 
 * public class XMLUtil{ }
 * package com.paladin.business;
 */
public class XMLUtil {  
  
private static Logger logger = LoggerFactory.getLogger(XMLUtil.class);

    private XMLUtil() {  
    }  
  
    /** 
     * 根据字符串创建 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;  
    }  
  
    /** 
     * 设置response的返回信息 
     * @param response 
     * @param fileName 
     * @return    XMLWriter 
     */  
    public static XMLWriter getXMLWriter(HttpServletResponse response,String fileName) {  
        XMLWriter writer = null;  
        OutputStream os = null;  
        try {  
            //定义xml文件的格式  
            OutputFormat format = OutputFormat.createPrettyPrint();  
            format.setEncoding("UTF-8");  
            //准备输出xml文件  
            writer =new XMLWriter(format);  
            response.setContentType("application/xml");  
            response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));  
            os = response.getOutputStream();  
            writer.setOutputStream(os);  
        }catch (Exception e){  
            logger.error("导出xml时获取XMLWriter出错:"+e);  
        }finally {  
            if(null != os){  
                try {  
                    os.close();  
                } catch (IOException e) {  
                    logger.error("关闭outputStream出错:"+e);  
                }  
            }  
        }  
        return writer;  
    }  
  
    /** 
     * 根据file获得Document 
     * @param file   MultipartFile 
     * @return       Document 
     */  
    public static Document getDocumentByFile(MultipartFile file){  
        File file1 = new File(file.getOriginalFilename());  
        Document document = null;  
        try {  
            file.transferTo(file1);  
            //创建SAXReader对象  
            SAXReader reader = new SAXReader();  
            //读取文件 转换成Document  
            document = reader.read(file1);  
        } catch (IOException e) {  
            document = null;  
            logger.error("文件转换出错:"+e);  
        } catch (DocumentException e) {  
            document = null;  
            logger.error("读取文件出错:"+e);  
        }  
        return document;  
    }  
    /** 
     * 导出模版,模版为xml格式 
     * @param response 
     * @param id 
     */  
    public static Result exportTmp(HttpServletResponse response,Document document,String name){
    if(StringUtils.isNotBlank(name)&&null!=document){
   
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值