java xml字符串变javabean javabean变xml字符串

此方法是JDK1.6以后的。

 

下面假如我们的javabean是下面这个类Withdrawingvo。

//根节点命名
@XmlRootElement(name="demo")
//@XmlAccessorType(XmlAccessType.FIELD) 写这个的话下面@XmlElement就不用写了
public class Withdrawingvo {
	@XmlElement
	public Head head;
	@XmlElement
	public String firmid;
	
	@XmlElement
	public String accountid;
	
	@XmlElement
	public String balance;
}

 

其中Head是个类:

//此类书与公用类,也就是很多的javabean都会用到它的属性,为了不必要的重写所以单拿出来。所以此类不是跟元素
public class Head {
	@XmlElement
	public String tranFunc;//交易类型
	
	@XmlElement
	public String servType;//服务类型
	
	@XmlElement
	public String tranDate;//交易日期
	
	@XmlElement
	public String tranTime;//交易时间
	
	@XmlElement
	public String rqstSeq;//请求方系统流水号
	
	@XmlElement
	public String qydm;//机构代码
}

 

 会用到的其他类:

Head的信息。然后根据head的信息来找到类。

@XmlRootElement(name = "demo")
public class UncertainMsg {
	@XmlElement
	public Head head;
}

 

public class TranFunc {
	//会员开销户确认
	public static final String RGST = "1002";
	//入金
	public static final String INMONEY = "1003";
	//出金
	public static final String OUTMONEY = "1004";
	//通讯检测
	public static final String CA="1001";

}

 

public class ServType {
	//请求
	public static final String RQST = "01";
	//响应
	public static final String RSP = "02";
}

 

public class ChoiceObject {
	/**
	 * key:业务代码 value:业务类
	 */
	private static Hashtable<String,Class> map=new Hashtable<String,Class>();
	
	static{
		//开户请求
		map.put(TranFunc.RGST+ServType.RQST, OpenAccountVo.class);
		//开户响应
		map.put(TranFunc.RGST+ServType.RSP, SocketResponse.class);
		//入金请求
		map.put(TranFunc.INMONEY+ServType.RQST, DepositVo.class);
		//入金响应
		map.put(TranFunc.INMONEY+ServType.RSP,SocketResponse.class);
		//出金请求 拿了此类举例。其他类结构和Withdrawingvo一样。但是没有往上写
		map.put(TranFunc.OUTMONEY+ServType.RQST,Withdrawingvo.class);
		//出金响应
		map.put(TranFunc.OUTMONEY+ServType.RSP,SocketResponse.class);
		//通讯检测请求
		map.put(TranFunc.CA+ServType.RQST, CAVO.class);
		//通讯检测响应
		map.put(TranFunc.CA+ServType.RSP,CAVO.class);
	}
	
	public static Class Choice(String tranFunc,String servType){
		return map.get(tranFunc+servType);
	}
}

 

下面是2中方法

 

//xml字符串转为类
	public Object getMsgObj(String receivemsg) {
		System.out.println("receivemsg==="+receivemsg);
		UncertainMsg uncertainMsg = null;
		JAXBContext context = null;
		try {
			context = JAXBContext.newInstance(UncertainMsg.class);
		} catch (JAXBException e) {
			throw new RuntimeException("报文头格式错误");
		}
		InputStream is=new ByteArrayInputStream(receivemsg.getBytes());
		Unmarshaller us;
		try {
			us=context.createUnmarshaller();
			//从指定的 InputStream 解组 XML 数据并返回得到的内容树。
			uncertainMsg=(UncertainMsg) us.unmarshal(is);
		} catch (JAXBException e) {
			System.out.println("创建Unmarshaller实例异常...");
			e.printStackTrace();
		}
		Object msgObj = null;
		try {
			context=JAXBContext.newInstance(ChoiceObject.Choice(uncertainMsg.head.tranFunc,uncertainMsg.head.servType));
		} catch (JAXBException e) {
			System.out.println("根据报头服务类型与交易类型获取操作类失败....");
			e.printStackTrace();
		}
		is=new ByteArrayInputStream(receivemsg.getBytes());
		try {
			us=context.createUnmarshaller();
			msgObj=us.unmarshal(is);
		} catch (JAXBException e) {
			
			e.printStackTrace();
		}
		return msgObj;
	}
	
	//类对象转为xml
	public String getMsgStr(Object msg) {
		JAXBContext context = null;
		Marshaller ms = null;
		try {
			context = JAXBContext.newInstance(msg.getClass());
			ms = context.createMarshaller();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		try {
			ms.setProperty(Marshaller.JAXB_ENCODING, ENCODING);
		} catch (PropertyException e) {
			e.printStackTrace();
		}
		Writer writer = new StringWriter();
		try {
			ms.marshal(msg, writer);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		String msgStr = writer.toString();
		return msgStr;
	}

 

当我们调用的时候以及使用的时候:

javabean--->xml

Withdrawingvo vo=new Withdrawingvo();
		vo.accountid="123";
		vo.balance="1000";
		vo.firmid="0001";
		Head head=new Head();
		head.qydm="市场1";
		head.rqstSeq="abasdfsaf";
		head.servType="01";
		head.tranFunc="1004";
		vo.head=head;
		String msg=cxml.getMsgStr(vo);

 

 

xml--->javabean

Object o=convert.getMsgObj(receive);
				//判断类对象是不是我要找的类。然后强转。使用的时候只要用类对象.出来就行
				if(o.getClass().isInstance(new Withdrawingvo())){
					Withdrawingvo withdraw=(Withdrawingvo) o;
					System.out.println("Withdrawingvo.class.firmid"+withdraw.firmid);
					System.out.println("Withdrawingvo.class.accountid"+withdraw.accountid);
					System.out.println("Withdrawingvo.class.balance"+withdraw.balance);
				}else if(o.getClass().isInstance(new OpenAccountVo())){
					........................

 

 

你可能感兴趣的:(javabean)