注解 | 使用范围 | 描述 |
@XmlAccessorOrder | 包、类 | 控制类中字段和属性的顺序。 |
@XmlAccessorType | 包、类 | 此注释提供对类中属性和字段的默认序列化的控制。 |
@XmlRootElement | 类、枚举 | 将类或枚举类型映射到XML元素。此注释可与以下注释一起使用:XmlType、XmlEnum、XmlAccessorType、XmlAccessorOrder。 |
@XmlElement | 字段 | 将JavaBean属性映射到从属性名称派生的XML元素 |
@XmlElementWrapper | 字段 | 围绕XML表示生成包装器元素。这主要用于围绕集合生成包装XML元素。此注释可与以下注释一起使用:XmlElement、XmlElements、XmlElementRef、XmlElementRefs、XmlJavaTypeAdapter。 |
@XmlAttribute | 字段 | 将JavaBean属性映射到XML属性。 |
@XmlValue | 字段 | 将字段或属性映射到 XML 标记上的文本值。 |
xml文件根元素的定义
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
@XmlRootElement(name = "Root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root implements Serializable {
private static final long serialVersionUID = 1L;
@XmlAttribute(name = "id")
private String id;
@XmlElementWrapper(name = "records")
@XmlElement(name = "record")
private List<Record> record;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Record> getRecord() {
return record;
}
public void setRecord(List<Record> record) {
this.record = record;
}
}
子节点下的类定义
import javax.xml.bind.annotation.XmlElement;
import java.io.Serializable;
import java.util.List;
public class Record implements Serializable {
private static final long serialVersionUID = 1L;
private List<Item> item;
public List<Item> getItem() {
return item;
}
@XmlElement(name = "item")
public void setItem(List<Item> item) {
this.item = item;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
import java.io.Serializable;
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String dataType;
private String perDataProId;
private String value;
public String getId() {
return id;
}
@XmlAttribute(name = "id")
public void setId(String id) {
this.id = id;
}
public String getDataType() {
return dataType;
}
@XmlAttribute(name = "dataType")
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getPerDataProId() {
return perDataProId;
}
@XmlAttribute(name = "perDataProId")
public void setPerDataProId(String perDataProId) {
this.perDataProId = perDataProId;
}
public String getValue() {
return value;
}
@XmlValue
public void setValue(String value) {
this.value = value;
}
}
xml数据结构格式
<Root id="Root">
<records>
<record id="">
<item id="field1" dataType="java.lang.String" perDataProId="">1</item>
<item id="field2" dataType="java.lang.String" perDataProId="">2</item>
<item id="field3" dataType="java.lang.String" perDataProId="">3</item>
<item id="field4" dataType="java.lang.String" perDataProId="">4</item>
<item id="field6" dataType="java.lang.String" perDataProId="">6</item>
<item id="field7" dataType="java.lang.String" perDataProId="">7</item>
</record>
</records>
</Root>
javaBean和xml转换工具类
import cn.hutool.json.JSONUtil;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
public class BeanXmlUtils {
/**
* bean转xml
* @param obj 需要转换的对象
* @param load 对应的class
* @param config isFormat:是否需要格式[默认:false],charSet:字符集[默认:utf-8],xmlHeader:是否需要xml头部[默认:false]
**/
public static String beanToXml(Object obj, Class<?> load,Object... config) {
String xmlStr = null;
try {
boolean isFormat = false;
String charSet = "utf-8";
boolean xmlHeader = false;
if (config.length>=1){
isFormat = (Boolean) config[0];
if (config.length>=2){
charSet = (String)config[1];
}
if (config.length>=3){
xmlHeader = (Boolean)config[2];
}
}
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, xmlHeader);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isFormat);
marshaller.setProperty(Marshaller.JAXB_ENCODING,charSet);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
xmlStr = writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlStr;
}
/**
* xml转bean
* @param str
* @param load
* @return T
**/
public static <T> T xmlToBean(String str, Class<T> load) {
Object object = null;
try {
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
object = unmarshaller.unmarshal(new StringReader(str));
} catch (JAXBException e) {
e.printStackTrace();
}
return (T)object;
}
public static void main(String[] args) {
//xmlToBean();
beanToXml();
}
private static void xmlToBean() {
String xmlStr = "<Root id=\"Root\">\n" +
"<records>\n" +
"\t<record id=\"\">\n" +
"\t<item id=\"field1\" dataType=\"java.lang.String\" perDataProId=\"\">1</item>\n" +
"\t<item id=\"field2\" dataType=\"java.lang.String\" perDataProId=\"\">2</item>\n" +
"\t<item id=\"field3\" dataType=\"java.lang.String\" perDataProId=\"\">3</item>\n" +
"\t<item id=\"field4\" dataType=\"java.lang.String\" perDataProId=\"\">4</item>\n" +
"\t<item id=\"field5\" dataType=\"java.lang.String\" perDataProId=\"\">5</item>\n" +
"\t<item id=\"field6\" dataType=\"java.lang.String\" perDataProId=\"\">6</item>\n" +
"\t<item id=\"field7\" dataType=\"java.lang.String\" perDataProId=\"\">7</item>\n" +
"\t</record>\n" +
"</records>\n" +
"</Root>";
Root root = xmlToBean(xmlStr, Root.class);
System.out.println(JSONUtil.toJsonPrettyStr(root));
}
private static void beanToXml() {
Root root = new Root();
root.setId("Root");
List<Record> records = new ArrayList<>();
List<Item> items = new ArrayList<>();
Item item = new Item();
item.setId("itemCode");
item.setDataType("java.lang.String");
item.setPerDataProId("");
item.setValue("TZ0001");
Item item1 = new Item();
item1.setId("itemName");
item1.setValue("吹风机");
items.add(item);
items.add(item1);
Record record = new Record();
record.setItem(items);
records.add(record);
root.setRecord(records);
String xmlStr = beanToXml(root, Root.class, false);
System.out.println(xmlStr);
}
}
更多【python-使用JAXB实现JavaBean与XML互相转换】相关视频教程:www.yxfzedu.com