Java:使用JAXB的注解实现定制xml结果

1. 声明

当前内容主要为测试和使用JAXB中的注解,实现定制xml,当前内容参考官方文档
主要包括:

  1. @XmlAccessorOrder、@XmlType (控制属性在xml中生成的顺序)
  2. @XmlJavaTypeAdapter (将某个属性进行定制转换xml)
  3. @XmlAttribute、@XmlValue (属性和值化属性)

2. 基本demo

public class AnntationTest {

	@XmlRootElement // 在生成xml的时候是必须的
	@XmlType(propOrder = { "city", "name", "map" }) 
	@XmlAccessorOrder(value = XmlAccessOrder.ALPHABETICAL) 
	public static class Country {
		private String name;
		private String city;
		private HashMap<Integer, String> map; 

		@XmlJavaTypeAdapter(value = UserXmlAdapter.class)	
		public HashMap<Integer, String> getMap() {
			return map;
		}

		public void setMap(HashMap<Integer, String> map) {
			this.map = map;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public Country(String name, String city) {
			super();
			this.name = name;
			this.city = city;
		}

		public Country() {
			super();
			// TODO Auto-generated constructor stub
		}

	}

	public static class MyHashMapEntryType {
		private Integer id;
		private String value;

		@XmlAttribute
		public Integer getId() {
			return id;
		}

		@XmlValue
		public String getValue() {
			return value;
		}

		public void setId(Integer id) {
			this.id = id;
		}

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

	}

	public static class MyHashMapType {

		private List<MyHashMapEntryType> entry;

		public List<MyHashMapEntryType> getEntry() {
			return entry;
		}

		public void setEntry(List<MyHashMapEntryType> entry) {
			this.entry = entry;
		}

	}

	public static class UserXmlAdapter extends XmlAdapter<MyHashMapType, HashMap<Integer, String>> {

		@Override
		public HashMap<Integer, String> unmarshal(MyHashMapType v) throws Exception {
			// TODO Auto-generated method stub
			if (v == null) {
				return null;
			}
			List<MyHashMapEntryType> entry = v.getEntry();
			if (entry == null || entry.isEmpty()) {
				return null;
			}
			System.out.println("parser xml to bean ===>");
			HashMap<Integer, String> map = new HashMap<>();
			for (MyHashMapEntryType myHashMapEntryType : entry) {
				map.put(myHashMapEntryType.getId(), myHashMapEntryType.getValue());
			}
			return map;
		}

		@Override
		public MyHashMapType marshal(HashMap<Integer, String> v) throws Exception {
			// TODO Auto-generated method stub
			if (v == null) {
				return null;
			}
			if (v.isEmpty()) {
				return null;
			}
			System.out.println("parser bean to xml ===>");
			MyHashMapType hashMapType = new MyHashMapType();
			List<MyHashMapEntryType> entryType = new ArrayList<AnntationTest.MyHashMapEntryType>();
			Set<Entry<Integer, String>> entrySet = v.entrySet();
			for (Entry<Integer, String> entry : entrySet) {
				Integer key = entry.getKey();
				String value = entry.getValue();
				MyHashMapEntryType myHashMapEntryType = new MyHashMapEntryType();
				myHashMapEntryType.setId(key);
				myHashMapEntryType.setValue(value);
				entryType.add(myHashMapEntryType);
			}
			hashMapType.setEntry(entryType);
			return hashMapType;
		}

	}

	public static void main(String[] args) throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(Country.class);
		Marshaller marshaller = jc.createMarshaller();
		/* marshaller.setAdapter(new UserXmlAdapter()); */
		// 添加了map才会在marshal的时候将其转换为xml中的map,如果map为null则不会出现该在xml中
		HashMap<Integer, String> map = new HashMap<>();
		map.put(1, "admin");
		map.put(2, "guest");
		// map.put(3, "user");
		Country country = new Country("test", "testCity");
		country.setMap(map);
		marshaller.marshal(country, System.out);
	}
}

结果:

parser bean to xml ===>

<country>
	<city>testCitycity>
	<name>testname>
	<map>
		<entry id="1">adminentry>
		<entry id="2">guestentry>
	map>
country>

3. 注解描述

1.@XmlType(propOrder = { "city", "name", "map" })其中propOrder用于定义属性名称在xml中的显示顺序,调整该顺序得到的xml结果顺序也会随之改变(注意:当前的属性必须在propOrder出现否则会报错)

属性map已存在, 但未在 @XmlType.propOrder 中指定 this problem is related to the following location: at public java.util.HashMap com.hy.java.xml.jaxb.AnntationTest$Country.getMap() at com.hy.java.xml.jaxb.AnntationTest$Country 属性mmm出现在 @XmlType.propOrder 中, 但不存在这种属性。您是否打算map? this problem is related to the following location: at com.hy.java.xml.jaxb.AnntationTest$Country

2.@XmlJavaTypeAdapter(value = UserXmlAdapter.class)为map这个属性设置转换器,按照转换器方式自动转换为另外一个对象,这个标记必须在对应的属性或该属性的get方法上面

3.@XmlAttribute、@XmlValue放在属性或属性对应的方法上面,主要是转换为admin的形式

4. 其他注解使用

其余的东西可以在Github上面中的
Java:使用JAXB的注解实现定制xml结果_第1张图片
该文档中具有JAXB注解的使用例子

你可能感兴趣的:(Java,xml,java,开发语言)