javax.xml 实现对象序列化与返序列化
目录结构:
根标签实体类Domain
package com.yh.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Domain")//如果不指定name 则标签名称默认使用类名+首字母小写。
@XmlAccessorType(XmlAccessType.FIELD) //支持XmlAttribute,XmlElement注解在 字段上,否则 XmlElement需要注解在get方法上
public class Domain {
@XmlAttribute //xml 属性注解,Domain标签的属性
private String type;
private String os;//简单字段
@XmlElement(name = "D") //指定标签名称,默认是属性名<d>。如果不用属性名,可以用此注解指定
private D d;//复合字段
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
public D getD() {
return d;
}
public void setD(D d) {
this.d = d;
}
@Override
public String toString() {
return "Domain{" +
"type="" + type + """ +
", os="" + os + """ +
", d=" + d +
"}";
}
}
/**
* 根标签下的复合子节点
*/
class D {
private String param1;
private String param2;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public String getParam2() {
return param2;
}
public void setParam2(String param2) {
this.param2 = param2;
}
@Override
public String toString() {
return "D{" +
"param1="" + param1 + """ +
", param2="" + param2 + """ +
"}";
}
}
package-info.java //命名空间设置(xmlns=”http://www.baidu.com”),本类放到实体类同层包内
//命名空间
@XmlSchema(namespace="http://www.baidu.com", elementFormDefault = XmlNsForm.QUALIFIED)
package com.yh.xml;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
demo 入口类XmlDemo:
package com.yh.xml;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
public class XmlDemo {
public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(Domain.class);
//复合对象
Domain domain = new Domain();//ROOT标签
D d = new D();//子标签-复合标签
d.setParam1("p1");
d.setParam2("p2");
domain.setType("kvm");//ROOT标签的属性
domain.setOs("linux");//子标签-简单类型标签
domain.setD(d);
/*Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(domain, System.out);//输出到控制台*/
System.out.println("=======序列化=======");
String marshal = marshal(domain.getClass(), domain);
System.out.println(marshal);
System.out.println("=======反序列化=======");
Domain domain1 = unmarshal(marshal, Domain.class);
System.out.println(domain1);
}
/**
* 把xml配置转换成对象
*
* @param xml
* @param classObj
* @return
*/
public static <T> T unmarshal(String xml, Class<T> classObj) {
T obj;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(classObj);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
obj = (T) unmarshaller.unmarshal(new StringReader(xml));
return obj;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
/**
* 把对象转换成xml配置
*
* @param classObj
* @param obj
* @return
*/
public static String marshal(Class<?> classObj, Object obj) {
String xmlStr = "";
try {
JAXBContext jaxbContext = JAXBContext.newInstance(classObj);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
//marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息
StringWriter out = new StringWriter();
marshaller.marshal(obj, new StreamResult(out));
xmlStr = out.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlStr;
}
}
执行结果: