JAXB
发布时间:2020-12-16 05:38:44 所属栏目:百科 来源:网络整理
导读:Java Architecture for XML Binding ( JAXB ) 主要用于Java对象和XML之间的相关转换,开始是作为一个独立项目开发,后来被集成到JDK中(自Java 6)。 JAXB Marshalling:将Java对象转换为XML JAXB Unmarshalling:将XML对象转换为Java Object JAXB主要使用注
Java Architecture for XML Binding ( JAXB ) 主要用于Java对象和XML之间的相关转换,开始是作为一个独立项目开发,后来被集成到JDK中(自Java 6)。
JAXB主要使用注释(annotations)来实现对象转换,比如,如果你需要将Java对象转换为XML,我们需要创建Marshaller对象,如果需要将XML转换为Java对象,则需要创建Unmarshaller对象。在JAXB中,你需要先创建JAXBContext对象提供了获取marshaller和unmarshall对象的方法一些常用的JAXB注释,官网中有更多JAXB中使用的注释。JAXB Official Site
<?xml version="1.0" encoding="UTF-8"?>
<root>
<requests>
<request id="123">
<url>http://www.baidu.com</url>
<cookie>username=test;logintime=20170202131415789;</cookie>
<encoding>utf-8</encoding>
</request>
</requests>
</root>
Java Beanimport javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Request {
private String url;
private String cookie;
private String encoding;
private int id;
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
@XmlElement
public void setUrl(String url) {
this.url = url;
}
public String getCookie() {
return cookie;
}
@XmlElement
public void setCookie(String cookie) {
this.cookie = cookie;
}
public String getEncoding() {
return encoding;
}
@XmlElement
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public String toString(){
return "id = "+id+",url = " + url+",cookie = " +cookie+",encoding = " + encoding;
}
}
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "root")
public class Requests {
private ArrayList<Request> requests;
public void addRequest(Request request){
if(requests == null){
requests = new ArrayList<Request>();
}
requests.add(request);
}
public ArrayList<Request> getRequests() {
return requests;
}
@XmlElementWrapper(name="requests")
@XmlElement(name="request")
public void setRequests(ArrayList<Request> requests) {
this.requests = requests;
}
}
XML To Bean@SuppressWarnings("unchecked")
public static <T> T xmlToBean(Class<T> bean,File file){
T s = null;
try{
JAXBContext context = JAXBContext.newInstance(bean);
Unmarshaller unmarshaller = context.createUnmarshaller();
s = (T)unmarshaller.unmarshal(file);
}catch(Exception e){
e.printStackTrace();
}
return s;
}
Java Object To XMLimport javax.xml.bind.annotation.XmlElement;
public class Result {
private String response;
private String url;
private String status;
public String getResponse() {
return response;
}
@XmlElement
public void setResponse(String response) {
this.response = response;
}
public String getUrl() {
return url;
}
@XmlElement
public void setUrl(String url) {
this.url = url;
}
public String getStatus() {
return status;
}
@XmlElement
public void setStatus(String status) {
this.status = status;
}
public String toString() {
return "status = " + status + ",url = " + url + ",response = " + response;
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="root")
public class Results {
private List<Result> results;
public List<Result> getResults() {
return results;
}
@XmlElementWrapper(name = "results")
@XmlElement(name="result")
public void setResults(List<Result> results) {
this.results = results;
}
}
Bean To XMLpublic static <T> String beanToXml(T bean){
String xmlStr = null;
StringWriter writer = null;
try{
JAXBContext context = JAXBContext.newInstance(bean.getClass());
Marshaller marshaller = context.createMarshaller();
writer = new StringWriter();
marshaller.marshal(bean,writer);
xmlStr = writer.toString();
}catch(Exception e){
e.printStackTrace();
}
return xmlStr;
}
public static <T> void beanToXml(String fileName,T bean){
try{
File file = new File(fileName);
JAXBContext context = JAXBContext.newInstance(bean.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(bean,file);
marshaller.marshal(bean,System.out);
}catch(Exception e){
e.printStackTrace();
}
}
result.xml这里写链接内容 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |