利用jdk6中Annotation将XML与对象之间互相转化(二)

5 篇文章 0 订阅

直接附源码

目录结构:



1、operations.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<operations>
	<operation type="03" value="38" analyTag="0000" analyDes="统计">
		<action type="2" value="1" subject="3" />
		<action type="b" value="a" subject="c" />
	</operation>
</operations>

2、OperationConfig.java

package com.xml.obj.test;

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.XmlRootElement;

/**
 * Created by admin on 2015/8/12.
 */
@XmlRootElement(name = "operations")
@XmlAccessorType(XmlAccessType.FIELD)
public class OperationConfig {
	@XmlElement(name = "operation")
	public List<Operation> list;

	public List<Operation> getList() {
		return list;
	}

	public void setList(List<Operation> list) {
		this.list = list;
	}
}

3、Operation.java

package com.xml.obj.test;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

/**
 * Created by admin on 2015/8/12.
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class Operation {
	@XmlElement(name = "action")
	List<Action> list;
	@XmlAttribute
	public String type;
	@XmlAttribute
	public String value;
	@XmlAttribute
	public String analyTag;
	@XmlAttribute
	public String analyDes;

	public List<Action> getList() {
		return list;
	}

	public void setList(List<Action> list) {
		this.list = list;
	}

	public String getAnalyTag() {
		return analyTag;
	}

	public void setAnalyTag(String analyTag) {
		this.analyTag = analyTag;
	}

	public String getAnalyDes() {
		return analyDes;
	}

	public void setAnalyDes(String analyDes) {
		this.analyDes = analyDes;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return "Operation [list=" + list + ", type=" + type + ", value=" + value + ", analyTag=" + analyTag + ", analyDes=" + analyDes + "]";
	}

}

4、Action.java

package com.xml.obj.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

/**
 * Created by admin on 2015/8/12.
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class Action {
	@XmlAttribute
	public String type;
	@XmlAttribute
	public String value;
	@XmlAttribute
	public String subject;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	@Override
	public String toString() {
		return "Action [type=" + type + ", value=" + value + ", subject=" + subject + "]";
	}

}

5、XmlParser.java

package com.xml.obj.test;

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.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * Created by admin on 2015/8/12.
 */
public class XmlParser {

	public static XmlParser insetance;

	private XmlParser() {

	}

	public static XmlParser getInstance() {
		if (insetance == null) {
			return new XmlParser();
		} else {
			return insetance;
		}

	}

	/* 将对象转为xml */
	public String mshall(Object oc, Class<?> cc) throws Exception {
		JAXBContext cxt = JAXBContext.newInstance(cc);
		Marshaller mm = cxt.createMarshaller();
		StringWriter writer = new StringWriter();
		mm.marshal(oc, writer);
		return writer.toString();

	}

	/* 将xml转对象 */
	public Object unmashall(String xmlString, Class<?> cc) throws Exception {
		JAXBContext cxt = JAXBContext.newInstance(cc);
		Unmarshaller unm = cxt.createUnmarshaller();
		Object cnt = unm.unmarshal(new StringReader(xmlString));
		return cnt;
	}

	public static void main(String[] args) throws Exception {

		test_unmashall();

		// test_mshall();

	}

	private static void test_mshall() {
		List<Action> list = new ArrayList<Action>();
		Action action = new Action();
		action.setValue("1");
		action.setType("2");
		action.setSubject("3");

		Action action2 = new Action();
		action2.setValue("a");
		action2.setType("b");
		action2.setSubject("c");
		list.add(action);
		list.add(action2);

		List<Operation> ops = new ArrayList<Operation>();
		Operation operation = new Operation();
		operation.setList(list);
		operation.setValue("38");
		operation.setType("03");
		operation.setAnalyTag("0000");
		operation.setAnalyDes("坐席违规统计");
		ops.add(operation);

		OperationConfig ff = new OperationConfig();
		ff.setList(ops);

		try {
			String xmlString = XmlParser.getInstance().mshall(ff, OperationConfig.class);
			System.out.println(xmlString);
		} catch (Exception e) {

		}

	}

	private static void test_unmashall() {
		String x1 = "<operations><operation type='03' value='38' analyTag='0000' analyDes='统计'></operation ></operations>";

		String x2 = "<operations><operation type='02' value='31' analyTag='0002' analyDes='统计2'><action type='1' value='2' subject='3'></action></operation></operations>";
		try {
			OperationConfig ff = (OperationConfig) XmlParser.getInstance().unmashall(x2, OperationConfig.class);

			List<Operation> ops = ff.getList();
			for (Operation op : ops) {
				System.out.println(op.toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
</pre><pre name="code" class="java">


源码:http://pan.baidu.com/s/1dD2NWad


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间辜负了谁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值