JDK17如何使用JAXB?

由于jdk9开始引入module功能,jaxb不再是jdk默认库的一部分,所以不能直接使用了,直接使用或者升级jdk会遇到各种异常,需要手工引入才行。
引入方法其实也非常简单: pom.xml加入依赖即可。 <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>4.0.3</version> </dependency>
下面是JAXB简单介绍及使用示例。

1.JAXB是什么?

通过JAXB可以方便实现JAVA对象与XML之间转换。
1.1 JAXBContext Jaxb的上下文,通过这个对象我们能拿到另外两个核心对象Unmarshaller(用于解析xml)和Marshaller(生成xml)
1.2 JAXBContext通常使用它的静态方法newInstance(Class className)来获得对象
1.3 Unmarshaller用于解析xml 通过JAXBContext的createUnmarshaller方法获得到
1.4 Marshaller用于生成xml 通过JAXBContext的createMarshaller方法获得到

2.1使用示例

public class XMLTemplateUtil { public static void main(String[] args) { String path1="D:\\zhubajie\\比赛软件\\橄榄球 马赛下注出票系统。\\TodayNRL-XML\\TodayNRL_1.xml"; File f1=new File(path1); try { f1.createNewFile(); JAXBContext jaxbContext = JAXBContext.newInstance(TodayNRLDto.class); TodayNRLDto t=new TodayNRLDto(); SessionDto s1=new SessionDto(); s1.setCode("1"); SessionDto s2=new SessionDto(); s2.setCode("2"); List<SessionDto> sList=new ArrayList<SessionDto>(); sList.add(s1); sList.add(s2); t.setSession(sList); jaxbContext.createMarshaller().marshal(t, f1); } catch (JAXBException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }

2.2生成的XML文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TodayNRL name="n1"> <session code="1" date="2023"> <abandoned>false</abandoned></session> <session code="2" date="2024"> <abandoned>false</abandoned> </session></TodayNRL>

2.3 Java Beans

@XmlRootElement(name="TodayNRL") @XmlAccessorType @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL) public class TodayNRLDto { private Collection<SessionDto> session; public TodayNRLDto() { } public Collection<SessionDto> getSession() { return session; } @XmlElement public void setSession(Collection<SessionDto> session) { this.session = session; } @Override public String toString() { return "TodayNRLDto{" + "session=" + session + '}'; } } package cn.bsdn.projs.betting.xml.dto; import jakarta.xml.bind.annotation.XmlAttribute; import jakarta.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "SESSION") public class SessionDto { private String date; private String round; private String name; private String code; private String t1; private String t2; private String type; private String status; private boolean abandoned; //Abandoned="false" Status=""> public SessionDto() { } public String getRound() { return round; } public void setRound(String round) { this.round = round; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getT1() { return t1; } public void setT1(String t1) { this.t1 = t1; } public String getT2() { return t2; } public void setT2(String t2) { this.t2 = t2; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public boolean isAbandoned() { return abandoned; } public void setAbandoned(boolean abandoned) { this.abandoned = abandoned; } public String getDate() { return date; } @XmlAttribute(name="DATE") public void setDate(String date) { this.date = date; } @Override public String toString() { return "SessionDto{" + "date=" + date + ", round=" + round + ", name=" + name + ", code=" + code + ", t1=" + t1 + ", t2=" + t2 + ", type=" + type + ", status=" + status + ", abandoned=" + abandoned + '}'; } }