利用jdk6中Annotation將XML與對象之間互相轉化(二)

直接附源碼

目錄結構:



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


發佈了132 篇原創文章 · 獲贊 55 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章