实体类与xml相互转化
2017-09-19 10:14 如是我所闻 阅读(295) 评论(0) 编辑 收藏 举报实体类:
package test; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
工具类:
package test; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; /** * 封装了XML转换成object,object转换成XML的代码 * * */ public class XMLUtil { /** * 将对象直接转换成String类型的 XML输出 * obj在class上必须加@XmlRootElement注释 * @param obj * @return */ public static String convertToXml(Object obj) { // 创建输出流 StringWriter sw = new StringWriter(); try { // 利用jdk中自带的转换类实现 JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // 格式化xml输出的格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // 将对象转换成输出流形式的xml marshaller.marshal(obj, sw); } catch (JAXBException e) { e.printStackTrace(); } return sw.toString(); } /** * 将对象根据路径转换成xml文件 * obj在class上必须加@XmlRootElement注释 * @param obj * @param path * @return */ public static void convertToXml(Object obj, String path) { try { // 利用jdk中自带的转换类实现 JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // 格式化xml输出的格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // 将对象转换成输出流形式的xml // 创建输出流 FileWriter fw = null; try { fw = new FileWriter(path); } catch (IOException e) { e.printStackTrace(); } marshaller.marshal(obj, fw); } catch (JAXBException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") /** * 将String类型的xml转换成对象 * clazz在类名上必须加@XmlRootElement注释 */ public static Object convertXmlStrToObject(Class clazz, String xmlStr) { Object xmlObject = null; try { JAXBContext context = JAXBContext.newInstance(clazz); // 进行将Xml转成对象的核心接口 Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader sr = new StringReader(xmlStr); xmlObject = unmarshaller.unmarshal(sr); } catch (JAXBException e) { e.printStackTrace(); } return xmlObject; } @SuppressWarnings("unchecked") /** * 将file类型的xml转换成对象 * clazz在类名上必须加@XmlRootElement注释 */ public static Object convertXmlFileToObject(Class clazz, String xmlPath) { Object xmlObject = null; try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); FileReader fr = null; try { fr = new FileReader(xmlPath); } catch (FileNotFoundException e) { e.printStackTrace(); } xmlObject = unmarshaller.unmarshal(fr); } catch (JAXBException e) { e.printStackTrace(); } return xmlObject; } public static void main(String[] args) { Student stu=new Student(); stu.setId(1); stu.setName("李三"); stu.setAge(20); //System.out.println(convertToXml(stu)); //convertToXml(stu, "e:\\alivin\\cc.xml"); /*String xmlStr="<student><age>20</age><id>1</id><name>李三</name></student>"; Student stu1=(Student)convertXmlStrToObject(Student.class, xmlStr); System.out.println(stu1);*/ System.out.println(convertXmlFileToObject(Student.class, "e:\\alivin\\cc.xml").toString()); } }