Jaxb工具类

平时我比较喜欢XML和java对象之间互转,同时每天要经常处理XML,所以我比较喜欢jaxb技术。下面是我对一些常用的简单封装。

 

package com.geostar.common.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
/**
 * @author likehua
 * @  工具类
 * */
public class JaxbUtil {
	private JaxbUtil(){};
	private  static Logger logger=Logger.getLogger(JaxbUtil.class);
	 //from object 2  xml
    public static Object   Xml2Object(Class<?> clazz,File file)throws JAXBException{
		JAXBContext context=JAXBContext.newInstance(clazz);
		Unmarshaller localUnmarshaller=context.createUnmarshaller();
		return  localUnmarshaller.unmarshal(file);
    	//return null;
	};
	public  static  Object Xml2Object(Class<?> clazz,File file,String schema)throws SAXException,JAXBException{
		 Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   
	      Schema sch = sf.newSchema(new  File(schema)); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(file);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
	    	throw  e;
	      }catch(JAXBException e){
	    	 logger.error(e);
	    	throw e;
	      }
	      return obj;		 
	};
	//from  xml  2  object
	public  static  void  Object2Xml(Object paramObject, NamespacePrefixMapper paramNamespacePrefixMapper, OutputStream paramOutputStream)throws JAXBException{
		Class localClass = paramObject.getClass();
	    JAXBContext localJAXBContext = JAXBContext.newInstance(new Class[] { localClass });
	    Marshaller localMarshaller = localJAXBContext.createMarshaller();
	    localMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", paramNamespacePrefixMapper);
	    localMarshaller.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
	    localMarshaller.marshal(paramObject, paramOutputStream);
	};
	//xml  2  object
	public static  Object Xml2Object(Class<?> clazz,InputStream is)throws JAXBException{
		JAXBContext context=JAXBContext.newInstance(clazz);
		Unmarshaller localUnmarshaller=context.createUnmarshaller();	
		return localUnmarshaller.unmarshal(is);
		//return null;
	};
	public  static  Object Xml2Object(Class<?> clazz,InputStream is,String schema)throws SAXException,JAXBException{
		 Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
	      Schema sch = sf.newSchema(new  File(schema)); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(is);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
		      throw e;
	      }catch(JAXBException e){
	    	  logger.error(e);
		      throw e;
	      }
	      return obj;		
	};
	//使用schema的url来验证jaxb
	public  static  Object Xml2Object(Class<?> clazz,InputStream is,URL  schema)throws SAXException,JAXBException{
		
		Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
		  //InputSource inSource=new InputSource(schema);
		  //SAXSource sax=new SAXSource(inSource);
	      Schema sch = sf.newSchema(schema); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(is);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
		      throw e;
	      }catch(JAXBException e){
	    	  logger.error(e);
		    	throw e;
	      }
	      return obj;		
	};
	//通过流来判断
	public  static  Object Xml2Object(Class<?> clazz,InputStream is,InputStream  schema)throws SAXException,JAXBException{
		 Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
		  InputSource inSource=new InputSource(schema);
		  SAXSource sax=new SAXSource(inSource);
	      Schema sch = sf.newSchema(sax); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(is);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
		    	throw e;
	      }catch(JAXBException e){
	    	  logger.error(e);
		    	throw e;
	      }
	      return obj;		
	};
  // 	xml  2  object
	public  static  Object  XML2Obejct(Class<?> clazz,Reader reader)throws JAXBException{
		JAXBContext context=JAXBContext.newInstance(new Class[]{clazz});
		Unmarshaller localMarshaller=context.createUnmarshaller();
		return localMarshaller.unmarshal(reader);
	};
	public  static  Object XML2Object(Class<?> clazz,Reader reader,InputStream schema)throws SAXException,JAXBException{
		//ss
		 Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   
		  InputSource inSource=new InputSource(schema);
		  SAXSource sax=new SAXSource(inSource);
	      Schema sch = sf.newSchema(sax); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(reader);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
		    	throw e;
	      }catch(JAXBException e){
	    	  logger.error(e);
		    	throw e;
	      }
	      return obj;	
	};
	public  static  Object  XML2Obejct(Class<?> clazz,Reader reader,String schema)throws SAXException,JAXBException{
		 Object obj = null ;
		 try{
		 ValidationEventCollector vec = new  ValidationEventCollector();  
		  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   
	      Schema sch = sf.newSchema(new  File(schema)); 
	      JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());   
	      Unmarshaller u = jc.createUnmarshaller();   
	      u.setSchema(sch);   
	      u.setEventHandler(vec);   
	      obj = u.unmarshal(reader);
		  }
	      catch(SAXException e){
	    	  logger.error(e);
		    	throw e;
	      }catch(JAXBException e){
	    	  logger.error(e);
		    	throw e;
	      }
	      return obj;		
	};
  // 	xml  2  object
	public static  void   Object2Xml(Object ob,OutputStream  os)throws JAXBException{
		Class local=ob.getClass();
		JAXBContext context=JAXBContext.newInstance(new Class[]{local});
		Marshaller marshaller=context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.marshal(ob, os);		
	};
//	from object 2 xml  file
    public static  void Object2XmlFile(Object ob, String path)throws JAXBException,FileNotFoundException {
		Class local=ob.getClass();
		JAXBContext context=JAXBContext.newInstance(new Class[]{local});
		Marshaller marshaller=context.createMarshaller();
		//marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", paramNamespacePrefixMapper);
		//marshaller.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.marshal(ob, new FileOutputStream(new File(path)));		
	};
//	from object 2 xml  file
    public static  void Object2XmlFile(Object ob,NamespacePrefixMapper paramNamespacePrefixMapper, String path)throws JAXBException,FileNotFoundException {
		Class local=ob.getClass();
		JAXBContext context=JAXBContext.newInstance(new Class[]{local});
		Marshaller marshaller=context.createMarshaller();
		marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", paramNamespacePrefixMapper);
		marshaller.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
		//.marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.marshal(ob, new FileOutputStream(new File(path)));
		//marshaller.marshal(ob, new File(path));
				
	};
	public static  void main(String[] args)throws FileNotFoundException,JAXBException,SAXException{
//		UpdateCSW cswRequest=(UpdateCSW)JaxbUtil.Xml2Object(UpdateCSW.class, new FileInputStream("C:\\request.xml"), UpdateCSW.class.getResourceAsStream("UpdateCSW.xsd"));
//		System.out.println("通过");
	};
}

转载于:https://my.oschina.net/lkh/blog/38653

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值