JAXB映射主要由四个部分组成:schema、 JAXB、 XML文档和Java对象。
citylist.xml
中 c
是跟节点使用@XmlRootElement
,provinceName
是属性使用@XmlAttribute
,
@XmlElement
对应object如下:
/**
* 城市列表
*
* @XmlRootElement 表示根节点
* @XmlAccessorType 控制字段或属性的序列化。FIELD表示将自动绑定java类中的每个非静态字段到XML
*
* @author szhu
*/
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
/**
* 省份名称
*
* @XmlAttribute 属性
*/
@XmlAttribute(name = "provinceName")
private String provinceName;
/**
* 城市列表
*
* @XmlElement 表示节点元素,可以嵌套,是城市的根节点
*/
@XmlElement(name = "d")
private List cityList;
public CityList() {
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public List getCityList() {
return cityList;
}
public void setCityList(List cityList) {
this.cityList = cityList;
}
}
中d 是跟节点使用@XmlRootElement
,d1
、d2
、d3
、d4
都是属性使用@XmlAttribute
。/**
* 城市
*
* @XmlRootElement 表示根节点
*
* @author szhu
*/
@XmlRootElement(name = "d")
@XmlAccessorType(XmlAccessType.FIELD)
public class City {
@XmlAttribute(name = "d1")
private String cityId;
@XmlAttribute(name = "d2")
private String cityName;
@XmlAttribute(name = "d3")
private String cityCode;
@XmlAttribute(name = "d4")
private String province;
public City() {
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
Unmarshaller.unmarshal( java.io.InputStream is )
支持InputStream,java可以很容易将文件转换为InputStream。所以可以这样使用
/**
* 将XML转为指定的POJO
*
* @param clazz 需要转换类型
* @param inputStream 流
* @return 对象
* @throws Exception
*/
static T xmlToObject(Class extends T> clazz, InputStream inputStream) throws Exception {
//通过映射的类创建XMLContext上下文对象,其中参数为映射的类
JAXBContext context = JAXBContext.newInstance(clazz);
//通过JAXBContext上下文对象创建createUnmarshaller()方法,创建XML转换成JAVA对象的格式
Unmarshaller unmarshaller = context.createUnmarshaller();
//将XML转换成对映的类,转换后需要强制性转换成映射的类
Object xmlObject = unmarshaller.unmarshal(inputStream);
//关闭流
inputStream.close();
//转换类型
return (T) xmlObject;
}
public static void main(String[] args) throws Exception {
//citylist.xml 转 inputStream
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("citylist.xml");
//inputStream 转 object
CityList cityList = xmlToObject(CityList.class, inputStream);
//打印
System.out.println(JSON.toJSONString(cityList));
}
输出:
{"cityList":[{"cityCode":"guangzhou","cityId":"101280101","cityName":"广州","province":"广东"},{"cityCode":"panyu","cityId":"101280102","cityName":"番禺","province":"广东"},{"cityCode":"conghua","cityId":"101280103","cityName":"从化","province":"广东"}]}
复用xml解析为object中的CityList
/**
* 对象转xml
* @param object 对象
* @param 对象类型,进行了bind
* @return xml
* @throws Exception
*/
static String objectToXml(T object) throws Exception {
//通过映射的类创建XMLContext上下文对象,其中参数为映射的类
JAXBContext context = JAXBContext.newInstance(object.getClass());
//通过JAXBContext上下文对象创建createMarshaller()方法,创建JAVA对象转xml的格式
Marshaller marshaller = context.createMarshaller();
//格式化输出,按标签自动换行,否则就一行输出
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//设置编码
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
//是否省略xml头部信息
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
sw.close();
return sw.toString();
}
public static void main(String[] args) throws Exception {
//构造 CityList数据
CityList cityList = getCityList();
//打印object
System.out.println(JSON.toJSONString(cityList));
//object转str
String xmlStr = objectToXml(cityList);
//打印
System.out.println(xmlStr);
}
输出
{"cityList":[{"cityCode":"guangzhou","cityId":"101280101","cityName":"广州","province":"广东"},{"cityCode":"panyu","cityId":"101280102","cityName":"番禺","province":"广东"},{"cityCode":"conghua","cityId":"101280103","cityName":"从化","province":"广东"}],"provinceName":"广东"}