• java操作xml超简单的方法


    用dom4j?SAX?no,no,no,光看api和帮助文档就烦,有没有更简单的方法呢?答案是有的。

    那就是默默无名的:JAXB

    jaxb是啥?

    摘抄一段度娘百科的介绍:

    JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式,常用的注解包括:@XmlRootElement,@XmlElement等等。
    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
     好的,下面开始介绍java 对象转换为xml
      1 import java.sql.Timestamp;
      2 import java.text.SimpleDateFormat;
      3 
      4 import javax.xml.bind.annotation.XmlRootElement;
      5 import javax.xml.bind.annotation.adapters.XmlAdapter;
      6 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
      7 
      8 /**
      9  * User entity. @author MyEclipse Persistence Tools
     10  */
     11 @XmlRootElement
     12 public class User implements java.io.Serializable {
     13 
     14     // Fields
     15 
     16     /**
     17      * 
     18      */
     19     private static final long serialVersionUID = 1L;
     20     private Integer id;
     21     private String username;
     22     private String password;
     23     private String sex;
     24 
     25     private Timestamp registTime;
     26 
     27     // Constructors
     28 
     29     /** default constructor */
     30     public User() {
     31     }
     32 
     33     /** full constructor */
     34     public User(String username, String password, String sex,
     35             Timestamp registTime) {
     36         this.username = username;
     37         this.password = password;
     38         this.sex = sex;
     39         this.registTime = registTime;
     40     }
     41 
     42     // Property accessors
     43 
     44     public Integer getId() {
     45         return this.id;
     46     }
     47 
     48     public void setId(Integer id) {
     49         this.id = id;
     50     }
     51 
     52     public String getUsername() {
     53         return this.username;
     54     }
     55 
     56     public void setUsername(String username) {
     57         this.username = username;
     58     }
     59 
     60     public String getPassword() {
     61         return this.password;
     62     }
     63 
     64     public void setPassword(String password) {
     65         this.password = password;
     66     }
     67 
     68     public String getSex() {
     69         return this.sex;
     70     }
     71 
     72     public void setSex(String sex) {
     73         this.sex = sex;
     74     }
     75 
     76     @XmlJavaTypeAdapter(DateAdapter.class)
     77     public Timestamp getRegistTime() {
     78         return this.registTime;
     79     }
     80 
     81     public void setRegistTime(Timestamp registTime) {
     82 
     83         this.registTime = registTime;
     84     }
     85 }
     86 
     87 /**
     88  * 日期类型得多花点操作和多个适配器
     89  * 
     90  * @author Administrator
     91  * 
     92  */
     93 class DateAdapter extends XmlAdapter<String, Timestamp> {
     94     private SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(
     95             "yyyy-MM-dd HH:mm:ss:SSS");
     96 
     97     @Override
     98     public Timestamp unmarshal(String v) throws Exception {
     99         return new Timestamp(yyyyMMddHHmmss.parse(v).getTime());
    100     }
    101 
    102     @Override
    103     public String marshal(Timestamp v) throws Exception {
    104         return yyyyMMddHHmmss.format(v);
    105     }
    106 }
    View Code

    main方法测试代码:

     1 public class Test1 {
     2     public static void main(String[] args) {
     3         File xmlFile = new File("src/user.xml");
     4         try {
     5 
     6             JAXBContext jc = JAXBContext.newInstance(User.class);
     7             
     8             Marshaller u=jc.createMarshaller();
     9             User user=new User();
    10             user.setId(0);
    11             user.setPassword(UUID.randomUUID().toString().replaceAll("-", ""));
    12             user.setRegistTime(new Timestamp(System.currentTimeMillis()));
    13             user.setSex("男");
    14             user.setUsername("1231123");
    15             u.marshal(user, xmlFile);
    16         } catch (Exception e) {
    17             // TODO: handle exception
    18         }
    19     }
    20 }
    View Code

    xml文件结果:

    1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    2 <user>
    3     <id>0</id>
    4     <password>2c892394522c41c29264899cfa9ea565</password>
    5     <registTime>2016-09-16 10:25:33:316</registTime>
    6     <sex></sex>
    7     <username>1231123</username>
    8 </user>
    View Code

    是不是很简单?不用dom4j,不用阅读各种api文档,不用网上搜索资料,只需要实体类上面加上注解@XmlRootElement就行了,下面介绍xml转换为java bean:

     1 import java.io.File;
     2 
     3 import javax.xml.bind.JAXBContext;
     4 import javax.xml.bind.Unmarshaller;
     5 
     6 import com.qhmcc.entity.User;
     7 
     8 public class Test1 {
     9     public static void main(String[] args) {
    10         File xmlFile = new File("src/user.xml");
    11         try {
    12 
    13             JAXBContext jc = JAXBContext.newInstance(User.class);
    14             
    15             Unmarshaller u=jc.createUnmarshaller();
    16             User usr=(User) u.unmarshal(xmlFile);
    17             System.out.println(usr.getUsername());
    18             System.out.println(usr.getPassword());
    19             System.out.println(usr.getSex());
    20             System.out.println(usr.getId());
    21             System.out.println(usr.getRegistTime());
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }
    25     }
    26 }
    View Code

    控制台结果如下:

    1 1231123
    2 2c892394522c41c29264899cfa9ea565
    3 4 0
    5 2016-09-16 10:25:33.316
    View Code

     ps:自定义XMl节点名字一定要在字段的get方法上面添加@XMLElement注解,一定要在get方法,别在字段上面,否则重复

  • 相关阅读:
    ansible二
    ansible一
    MySQL索引
    MySQL binlog server原理与搭建
    MySQL online DDL 白话
    MySQL online ddl原理
    Microsoft Visual C++ 14.0 is required解决方法
    man的汉化及使用
    对象,类,命名空间,继承......
    PHP初识
  • 原文地址:https://www.cnblogs.com/swtjavaspace/p/5876026.html
Copyright © 2020-2023  润新知