由java对象生成xml文件实例
注意文件SmCitysVO.java, SmCityVO.java, SmDeptVO.java.
1.pom文件引入jar包:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
2.SmCitysVO.java内容:
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "document")
public class SmCitysVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3179715019863423515L;
private List<SmCityVO> citys;
/**
* @return the stores
*/
@XmlElementWrapper(name = "citys")
@XmlElements({@XmlElement(name="city", type=SmCityVO.class)})
public List<SmCityVO> getCitys() {
return citys;
}
public void setCitys(List<SmCityVO> citys) {
this.citys = citys;
}
}
3.SmCityVO.java内容:
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "city")
//@XmlType(propOrder = { "city_code", "city_name", "stores"})
public class SmCityVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4351598442281853801L;
private String cityCode;//城市id
private String cityName;//城市名字
private String cityExplain;//热门城市,………………
private List<SmDeptVO> stores;//门店列表
private String sourceName = ""; //
@XmlElement(name = "city_code")
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
@XmlElement(name = "city_name")
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
/**
* @return the stores
*/
@XmlElementWrapper(name = "stores")
@XmlElements({@XmlElement(name="store", type=SmDeptVO.class)})
public List<SmDeptVO> getStores() {
return stores;
}
/**
* @param stores the stores to set
*/
public void setStores(List<SmDeptVO> stores) {
this.stores = stores;
}
/**
* @return the sourceName
*/
@XmlElement(name = "source_name")
public String getSourceName() {
return sourceName;
}
/**
* @param sourceName the sourceName to set
*/
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
/**
* @return the cityExplain
*/
@XmlElement(name = "city_explain")
public String getCityExplain() {
return cityExplain;
}
/**
* @param cityExplain the cityExplain to set
*/
public void setCityExplain(String cityExplain) {
this.cityExplain = cityExplain;
}
}
4.SmDeptVO.java内容:
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "store")
public class SmDeptVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4351598442281853801L;
private String district; //区
private String areaStoreCode;//门店id
private String areaStore;//门店名称
private String areaStoreAddress; //门店地址
private String areaStoreLongitude;//门店经纬度
private String areaStoreLatitude;//门店经纬度
private Integer status = 1; //是否有效,0:无效,1有效。默认是有效的
private String url; //索引
@XmlElement(name = "district")
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
@XmlElement(name = "area_store_code")
public String getAreaStoreCode() {
return areaStoreCode;
}
public void setAreaStoreCode(String areaStoreCode) {
this.areaStoreCode = areaStoreCode;
}
@XmlElement(name = "area_store_address")
public String getAreaStoreAddress() {
return areaStoreAddress;
}
public void setAreaStoreAddress(String areaStoreAddress) {
this.areaStoreAddress = areaStoreAddress;
}
@XmlElement(name = "area_store_longitude")
public String getAreaStoreLongitude() {
return areaStoreLongitude;
}
public void setAreaStoreLongitude(String areaStoreLongitude) {
this.areaStoreLongitude = areaStoreLongitude;
}
@XmlElement(name = "area_store_latitude")
public String getAreaStoreLatitude() {
return areaStoreLatitude;
}
public void setAreaStoreLatitude(String areaStoreLatitude) {
this.areaStoreLatitude = areaStoreLatitude;
}
/**
* @return the areaStore
*/
@XmlElement(name = "area_store")
public String getAreaStore() {
return areaStore;
}
/**
* @param areaStore the areaStore to set
*/
public void setAreaStore(String areaStore) {
this.areaStore = areaStore;
}
/**
* @return the status
*/
public Integer getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(Integer status) {
this.status = status;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
}
5.对象生成 xml文件方法:
public void createDepInfoxml(String filePath) {
try {
LOGGER.debug("生成depinfo文件方法");
SmCitysVO citys = ;//组装数据
JAXBContext jaxbContext = JAXBContext.newInstance(SmCitysVO.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
String depInfPaht = filePath + "/depinfo.xml";
LOGGER.debug("depInfPaht路径,{}", depInfPaht);
jaxbMarshaller.marshal(citys, new File(depInfPaht)); //生成门店信息文件
createSiteMapxml(filePath);//生成门店信息成功,去生成sitemap文件
} catch (Exception e) {
LOGGER.error("生成depinfo文件方法异常,{}", e.getMessage());
e.printStackTrace();
}
}
注意文件SmCitysVO.java, SmCityVO.java, SmDeptVO.java.
1.pom文件引入jar包:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
2.SmCitysVO.java内容:
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "document")
public class SmCitysVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3179715019863423515L;
private List<SmCityVO> citys;
/**
* @return the stores
*/
@XmlElementWrapper(name = "citys")
@XmlElements({@XmlElement(name="city", type=SmCityVO.class)})
public List<SmCityVO> getCitys() {
return citys;
}
public void setCitys(List<SmCityVO> citys) {
this.citys = citys;
}
}
3.SmCityVO.java内容:
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "city")
//@XmlType(propOrder = { "city_code", "city_name", "stores"})
public class SmCityVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4351598442281853801L;
private String cityCode;//城市id
private String cityName;//城市名字
private String cityExplain;//热门城市,………………
private List<SmDeptVO> stores;//门店列表
private String sourceName = ""; //
@XmlElement(name = "city_code")
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
@XmlElement(name = "city_name")
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
/**
* @return the stores
*/
@XmlElementWrapper(name = "stores")
@XmlElements({@XmlElement(name="store", type=SmDeptVO.class)})
public List<SmDeptVO> getStores() {
return stores;
}
/**
* @param stores the stores to set
*/
public void setStores(List<SmDeptVO> stores) {
this.stores = stores;
}
/**
* @return the sourceName
*/
@XmlElement(name = "source_name")
public String getSourceName() {
return sourceName;
}
/**
* @param sourceName the sourceName to set
*/
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
/**
* @return the cityExplain
*/
@XmlElement(name = "city_explain")
public String getCityExplain() {
return cityExplain;
}
/**
* @param cityExplain the cityExplain to set
*/
public void setCityExplain(String cityExplain) {
this.cityExplain = cityExplain;
}
}
4.SmDeptVO.java内容:
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Description: 城市门店信息
* All Rights Reserved.
* @version 1.0 2015-12-30 下午1:35:50 by
*/
@XmlRootElement(name = "store")
public class SmDeptVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4351598442281853801L;
private String district; //区
private String areaStoreCode;//门店id
private String areaStore;//门店名称
private String areaStoreAddress; //门店地址
private String areaStoreLongitude;//门店经纬度
private String areaStoreLatitude;//门店经纬度
private Integer status = 1; //是否有效,0:无效,1有效。默认是有效的
private String url; //索引
@XmlElement(name = "district")
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
@XmlElement(name = "area_store_code")
public String getAreaStoreCode() {
return areaStoreCode;
}
public void setAreaStoreCode(String areaStoreCode) {
this.areaStoreCode = areaStoreCode;
}
@XmlElement(name = "area_store_address")
public String getAreaStoreAddress() {
return areaStoreAddress;
}
public void setAreaStoreAddress(String areaStoreAddress) {
this.areaStoreAddress = areaStoreAddress;
}
@XmlElement(name = "area_store_longitude")
public String getAreaStoreLongitude() {
return areaStoreLongitude;
}
public void setAreaStoreLongitude(String areaStoreLongitude) {
this.areaStoreLongitude = areaStoreLongitude;
}
@XmlElement(name = "area_store_latitude")
public String getAreaStoreLatitude() {
return areaStoreLatitude;
}
public void setAreaStoreLatitude(String areaStoreLatitude) {
this.areaStoreLatitude = areaStoreLatitude;
}
/**
* @return the areaStore
*/
@XmlElement(name = "area_store")
public String getAreaStore() {
return areaStore;
}
/**
* @param areaStore the areaStore to set
*/
public void setAreaStore(String areaStore) {
this.areaStore = areaStore;
}
/**
* @return the status
*/
public Integer getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(Integer status) {
this.status = status;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
}
5.对象生成 xml文件方法:
public void createDepInfoxml(String filePath) {
try {
LOGGER.debug("生成depinfo文件方法");
SmCitysVO citys = ;//组装数据
JAXBContext jaxbContext = JAXBContext.newInstance(SmCitysVO.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
String depInfPaht = filePath + "/depinfo.xml";
LOGGER.debug("depInfPaht路径,{}", depInfPaht);
jaxbMarshaller.marshal(citys, new File(depInfPaht)); //生成门店信息文件
createSiteMapxml(filePath);//生成门店信息成功,去生成sitemap文件
} catch (Exception e) {
LOGGER.error("生成depinfo文件方法异常,{}", e.getMessage());
e.printStackTrace();
}
}