1.準備xml檔案
我是将xml檔案放在resources下面的

以下是xml檔案的内容,我截取了一部分
<?xml version="1.0" encoding="UTF-8"?>
<c c1="0">
<d d1="101280101" d2="廣州" d3="guangzhou" d4="廣東"/>
<d d1="101280102" d2="番禺" d3="panyu" d4="廣東"/>
<d d1="101280103" d2="從化" d3="conghua" d4="廣東"/>
<d d1="101280104" d2="增城" d3="zengcheng" d4="廣東"/>
<d d1="101280105" d2="花都" d3="huadu" d4="廣東"/>
<d d1="101280201" d2="韶關" d3="shaoguan" d4="廣東"/>
</c>
2.準備實體類City 和CityList
下面是City的代碼
package com.example.weather.vo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@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 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;
}
}
下面是CityList的代碼
package com.example.weather.vo;
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlRootElement(name = "c")
@XmlAccessorType(XmlAccessType.FIELD)
public class CityList {
@XmlElement(name = "d")
private List<City> cityList;
public List<City> getCityList(){
return cityList;
}
public void setCityList(List<City> cityList){
this.cityList = cityList;
}
}
3.建立工具類XmlBuilder
以下是 XmlBuilder 的代碼
package com.example.weather.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;
public class XmlBuilder {
public static Object xmlStrToObject(Class<?> clazz, String xmlStr) throws Exception{
Object xmlObject = null;
Reader reader = null;
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(reader);
return xmlObject;
}
}
4.在對應的 service 調用
以下是 CityDataServiceImpl 代碼
package com.example.weather.service.impl;
import com.example.weather.service.CityDataService;
import com.example.weather.util.XmlBuilder;
import com.example.weather.vo.City;
import com.example.weather.vo.CityList;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import javax.xml.ws.ServiceMode;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Service
public class CityDataServiceImpl implements CityDataService {
@Override
public List<City> listCity() throws Exception {
//讀取XML檔案
Resource resource = new ClassPathResource("citylist.xml");
BufferedReader bf = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();
String line = "";
while((line = bf.readLine()) != null){
builder.append(line);
}
bf.close();
//XML轉為java對象
CityList cityList = (CityList)XmlBuilder.xmlStrToObject(CityList.class, builder.toString());
return cityList.getCityList();
}
}
5.controller我就不上代碼了,我用的thymeleaf做模闆引擎,效果如下