博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAXB简单样例
阅读量:6859 次
发布时间:2019-06-26

本文共 3535 字,大约阅读时间需要 11 分钟。

参考网页:http://www.mkyong.com/java/jaxb-hello-world-example/

JAXB完整教程:https://jaxb.java.net/tutorial/
1.JAXB 依赖
maven项目使用JAXB首先需要添加jaxb api和jaxb impl,jaxb core(因为jaxb api 2.2.11 版本之后不再支持annomationReader,详见:http://stackoverflow.com/questions/27046836/why-has-annotationreader-been-removed-from-jaxb-reference-implementation):
jaxb api的maven dependency可以在这里找到:http://mvnrepository.com/artifact/javax.xml/jaxb-api
jaxb impl的maven dependency可以在这里找到:http://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
jaxb core的maven dependency可以在这里找到:http://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core
这里使用最新的版本,添加如下代码到pom.xml的dependencies当中:

com.sun.xml.bind
jaxb-core
2.2.11
javax.xml
jaxb-api
2.1
com.sun.xml.bind
jaxb-impl
2.2.11

2.JAXB注释

需要进行xml转换的类必须用JAXB注释(JAXB annotation)进行注释。

package com.mypackage;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Customer {    String name;    int age;    int id;    public String getName() {        return name;    }    @XmlElement    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    @XmlElement    public void setAge(int age) {        this.age = age;    }    public int getId() {        return id;    }    @XmlAttribute    public void setId(int id) {        this.id = id;    }    @Override    public String toString() {        return "Customer [name=" + name + ", age=" + age + ", id=" + id + "]";    }}

3.将对象转换成XML

JAXB编组(marshall)对象成XML。

package com.mypackage;import java.io.File;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;public class JAXBMarshallExample {    public static void main(String[] args) {        Customer customer = new Customer();        customer.setId(1000);        customer.setName("moon");        customer.setAge(18);                try {            File file = new File("C:\\file.xml");            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();                        // output pretty printed            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);                        jaxbMarshaller.marshal(customer, file);            jaxbMarshaller.marshal(customer, System.out);        } catch (JAXBException e) {            e.printStackTrace();        }    }}

输出结果:

18
moon

4.将XML转换成对象

JAXB解组(unmarshall)XML成对象。

package com.mypackage;import java.io.File;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;public class JAXBUnmarshallExample {    public static void main(String[] args) {        try {            File file = new File("C:\\file.xml");            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);                        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();            Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);            System.out.println(customer);        } catch (JAXBException e) {            e.printStackTrace();        }    }}

程序输出:

Customer [name=moon, age=18, id=1000]

 

转载地址:http://ibxyl.baihongyu.com/

你可能感兴趣的文章
gitlab的使用
查看>>
iOS 生成本地验证码
查看>>
找不到 javax.servlet.http.HttpServletResponse 和 javax.servlet.http.HttpServletRequest 问题解决...
查看>>
Flip Game(枚举)
查看>>
WebWorker与WebSocket实现前端消息总线
查看>>
Selector
查看>>
Unity 2018.3.1 SyncVar没有同步服务器变量
查看>>
Linux命令(2) - 查看目录和文件大小: du -sh
查看>>
python的一些常用标准库
查看>>
最短路径--Floyd、Dijkstra、Bellman、SPFA算法
查看>>
gunzip
查看>>
使用CAShapeLayer绘制小人
查看>>
6.用递归实现求数组中的任意次最值。
查看>>
清华大学MBA在职班第一学年第二学期课表
查看>>
公开课:如何成为一名高级系统运维工程师(架构师)?
查看>>
深入Hadoop节点部署的策略
查看>>
Linux下高效数据恢复软件extundelete应用实战
查看>>
【REACT NATIVE 系列教程之五】NAVIGATOR(页面导航)的基本使用与传参
查看>>
由浅入深学优化之like‘%%’坑爹写法
查看>>
部署Hadoop高性能集群
查看>>