参考:
https://blog.csdn.net/LookForDream_/article/details/88884316
https://zhuchengzzcc.iteye.com/blog/1838702
核心代码
1import java.io.File; 2import java.io.FileReader; 3import java.io.IOException; 4import java.io.StringReader; 5import java.io.StringWriter; 6 7import javax.xml.bind.JAXBContext; 8import javax.xml.bind.Marshaller; 9import javax.xml.bind.Unmarshaller; 10 11import org.slf4j.Logger; 12import org.slf4j.LoggerFactory; 13 14import dns.rsdnscount.entity.SlaveEntity; 15import dns.rsdnscount.entity.SlavesEntity; 16 17 18public class XmlUtils { 19 20 private static Logger logger = LoggerFactory.getLogger(XmlUtils.class); 21 22 public static void main(String[] args) { 23/* SlavesEntity list=new SlavesEntity(); 24 25 SlaveEntity ent=new SlaveEntity(); 26 ent.setHost("master"); 27 ent.setPort("3306"); 28 ent.setUserName("user1"); 29 ent.setRemoteResultPath("/home/user"); 30 list.getEntitys().add(ent); 31 SlaveEntity ent2=new SlaveEntity(); 32 ent2.setHost("master2"); 33 ent2.setPort("3306"); 34 ent2.setUserName("user2"); 35 ent2.setRemoteResultPath("/home/user2"); 36 list.getEntitys().add(ent2); 37 String xxx=convertToXml(list,"utf-8",true); 38 System.out.println(xxx);*/ 39 try { 40 SlavesEntity ents=convertToJava(new File("conf/slaves.xml"), SlavesEntity.class); 41 for (SlaveEntity ent : ents.getEntityList()) { 42 System.out.println(ent); 43 } 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 49 50 } 51 52 /** 53 * JavaBean转换成xml 54 * 55 * @param obj 56 * @param encoding 57 * @return 58 */ 59 public static String convertToXml(Object obj, String encoding, boolean format) { 60 String result = null; 61 StringWriter writer = null; 62 try { 63 JAXBContext context = JAXBContext.newInstance(obj.getClass()); 64 Marshaller marshaller = context.createMarshaller(); 65 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); 66 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); 67 writer = new StringWriter(); 68 marshaller.marshal(obj, writer); 69 result = writer.toString(); 70 } catch (Exception e) { 71 logger.error(e.getMessage(), e); 72 } finally { 73 if (writer != null){ 74 try { 75 writer.close(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 } 81 return result; 82 } 83 84 /** 85 * xml转换成JavaBean 86 * 87 * @param xml 88 * @param c 89 * @return 90 */ 91 @SuppressWarnings("unchecked") 92 public static <T> T convertToJava(String xml, Class<T> c) { 93 if (xml == null || xml.equals("")) 94 return null; 95 T t = null; 96 StringReader reader = null; 97 try { 98 JAXBContext context = JAXBContext.newInstance(c); 99 Unmarshaller unmarshaller = context.createUnmarshaller(); 100 reader = new StringReader(xml); 101 t = (T) unmarshaller.unmarshal(reader); 102 } catch (Exception e) { 103 logger.error(e.getMessage(), e); 104 } finally { 105 if (reader != null) 106 reader.close(); 107 } 108 return t; 109 } 110 111 @SuppressWarnings("unchecked") 112 public static <T> T convertToJava(File filePath, Class<T> c) throws IOException { 113 if (!filePath.exists()) 114 return null; 115 T t = null; 116 FileReader reader = null; 117 try { 118 JAXBContext context = JAXBContext.newInstance(c); 119 Unmarshaller unmarshaller = context.createUnmarshaller(); 120 reader = new FileReader(filePath); 121 t = (T) unmarshaller.unmarshal(reader); 122 } catch (Exception e) { 123 logger.error(e.getMessage(), e); 124 } finally { 125 if (reader != null) 126 reader.close(); 127 } 128 return t; 129 } 130}
实体类
1import java.util.LinkedList; 2 3import javax.xml.bind.annotation.XmlAccessType; 4import javax.xml.bind.annotation.XmlAccessorType; 5import javax.xml.bind.annotation.XmlElement; 6import javax.xml.bind.annotation.XmlElementWrapper; 7import javax.xml.bind.annotation.XmlRootElement; 8 9@XmlAccessorType(XmlAccessType.FIELD) 10@XmlRootElement(name="slavesList") 11public class SlavesEntity{ 12 13 @XmlElementWrapper(required=true,name="entitys") 14 @XmlElement(name="entity") 15 LinkedList<SlaveEntity> entityList=new LinkedList<SlaveEntity>(); 16 17 public LinkedList<SlaveEntity> getEntityList() { 18 return entityList; 19 } 20 21 public void setEntityList(LinkedList<SlaveEntity> entityList) { 22 this.entityList = entityList; 23 } 24 25} 26 27import javax.xml.bind.annotation.XmlAccessType; 28import javax.xml.bind.annotation.XmlAccessorType; 29import javax.xml.bind.annotation.XmlElement; 30import javax.xml.bind.annotation.XmlType; 31 32@XmlAccessorType(XmlAccessType.FIELD) 33@XmlType(propOrder = { 34 "host", 35 "port", 36 "userName", 37 "remoteResultPath", 38}) 39public class SlaveEntity { 40 41 String host; 42 String userName; 43 String port; 44 @XmlElement(required=false,name="remoteResultPath") 45 String remoteResultPath; 46 47 public String getHost() { 48 return host; 49 } 50 public void setHost(String host) { 51 this.host = host; 52 } 53 public String getUserName() { 54 return userName; 55 } 56 public void setUserName(String userName) { 57 this.userName = userName; 58 } 59 public String getPort() { 60 return port; 61 } 62 public void setPort(String port) { 63 this.port = port; 64 } 65 public String getRemoteResultPath() { 66 return remoteResultPath; 67 } 68 public void setRemoteResultPath(String remoteResultPath) { 69 this.remoteResultPath = remoteResultPath; 70 } 71 @Override 72 public String toString() { 73 return "SlaveEntity [host=" + host + ", userName=" + userName + ", port=" + port + ", remoteResultPath=" 74 + remoteResultPath + "]"; 75 } 76}