Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.zhangmz.cymbidium.modules.convert; import org.junit.Assert; import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlType; import org.apache.commons.lang3.builder.ToStringBuilder; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.junit.Test; import com.google.common.collect.Lists; /** * JAXB2.0Java-XML??Dom4j. * * xml: * * <pre> * <?xml version="1.0" encoding="UTF-8"?> * <user id="1"> * <name>calvin</name> * <interests> * <interest>movie</interest> * <interest>sports</interest> * </interests> * </user> * </pre> * * @author: * @date:2016124 ?9:29:38 * @version V1.0 * springside? * org.assertj.core.api.Assertions.* --> org.junit.* */ public class JaxbMapperTest { @Test public void objectToXml() { User user = new User(); user.setId(1L); user.setName("calvin"); user.getInterests().add("movie"); user.getInterests().add("sports"); String xml = JaxbMapper.toXml(user, "UTF-8"); System.out.println("Jaxb Object to Xml result:\n" + xml); } @Test public void xmlToObject() { String xml = generateXmlByDom4j(); User user = JaxbMapper.fromXml(xml, User.class); System.out.println("Jaxb Xml to Object result:\n" + user); Assert.assertEquals(1L, user.getId().longValue()); // ? List expecteds = Lists.newArrayList("movie", "sports"); Assert.assertTrue(expecteds.equals(user.getInterests())); } /** * ListXML */ @Test public void toXmlWithListAsRoot() { User user1 = new User(); user1.setId(1L); user1.setName("calvin"); User user2 = new User(); user2.setId(2L); user2.setName("kate"); List<User> userList = Lists.newArrayList(user1, user2); String xml = JaxbMapper.toXml(userList, "userList", User.class, "UTF-8"); System.out.println("Jaxb Object List to Xml result:\n" + xml); } /** * Dom4j?XML. */ private static String generateXmlByDom4j() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("user").addAttribute("id", "1"); root.addElement("name").setText("calvin"); // List<String> Element interests = root.addElement("interests"); interests.addElement("interest").addText("movie"); interests.addElement("interest").addText("sports"); return document.asXML(); } /** * Dom4j?Jaxb?XML. */ private static void assertXmlByDom4j(String xml) { Document doc = null; try { doc = DocumentHelper.parseText(xml); } catch (DocumentException e) { System.out.println(e.getMessage()); } Element user = doc.getRootElement(); Assert.assertEquals("1", user.attribute("id").getValue()); Element interests = (Element) doc.selectSingleNode("//interests"); Assert.assertTrue(interests.elements().size() == 2); Assert.assertEquals("movie", ((Element) interests.elements().get(0)).getText()); } @XmlRootElement // ?? @XmlType(propOrder = { "name", "interests" }) private static class User { private Long id; private String name; private String password; private List<String> interests = Lists.newArrayList(); // ?xml @XmlAttribute public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } // ??xml @XmlTransient public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } // List<String>, xml<interests><interest>movie</interest></interests> @XmlElementWrapper(name = "interests") @XmlElement(name = "interest") public List<String> getInterests() { return interests; } public void setInterests(List<String> interests) { this.interests = interests; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } }