Java对象转XML格式字符串
XML中标签字段不设置顺序
定义Java类,使用@XmlRootElement和@XmlElement注解定义XML中的标签名称,通过set方法设置XML标签中的内容
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Document")
public class Message {
private MsgHeader header;
private MsgBody body;
@XmlElement(name = "head")
public MsgHeader getHeader() {
return header;
}
public void setHeader(MsgHeader header) {
this.header = header;
}
@XmlElement(name = "body")
public MsgBody getBody() {
return body;
}
public void setBody(MsgBody body) {
this.body = body;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "head")
public class MsgHeader {
private String id;
private String type;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "body")
public class MsgBody {
private String content;
private String date;
@XmlElement(name = "content")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@XmlElement(name = "date")
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
/**
* 测试类
*/
public class Test {
public static void main(String[] args) {
Message message = new Message();
MsgHeader header = new MsgHeader();
MsgBody body = new MsgBody();
header.setId("1");
header.setType("A");
body.setDate("20210506");
body.setContent("qwerty");
message.setHeader(header);
message.setBody(body);
StringWriter sw = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(message.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(message, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
System.out.println(sw);
}
}
输出结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
<body>
<content>qwerty</content>
<date>20210506</date>
</body>
<head>
<id>1</id>
<type>A</type>
</head>
</Document>
XML中标签字段设置顺序
上面的定义方式生成的XML字符串默认按标签字母排序,可以通过@XmlType注解来手动设置XML中标签的顺序,propOrder中设置的是类的字段名称。
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Document")
@XmlType(propOrder = {"header", "body"})
public class Message {
private MsgHeader header;
private MsgBody body;
@XmlElement(name = "head")
public MsgHeader getHeader() {
return header;
}
public void setHeader(MsgHeader header) {
this.header = header;
}
@XmlElement(name = "body")
public MsgBody getBody() {
return body;
}
public void setBody(MsgBody body) {
this.body = body;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "head")
@XmlType(propOrder = {"id", "type"})
public class MsgHeader {
private String id;
private String type;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "body")
@XmlType(propOrder = {"date", "content"})
public class MsgBody {
private String content;
private String date;
@XmlElement(name = "content")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@XmlElement(name = "date")
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
再次运行上面的测试类,输出结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
<head>
<id>1</id>
<type>A</type>
</head>
<body>
<date>20210506</date>
<content>qwerty</content>
</body>
</Document>