友情提醒:
先看文章目錄,大致了解文章知識(shí)點(diǎn)結(jié)構(gòu),點(diǎn)擊文章目錄可直接跳轉(zhuǎn)到文章指定位置。有用記得關(guān)注
第一章、功能需求和分析
1.1)具體需求
解析XML字符串,并將解析后得到的結(jié)果,按時(shí)存入數(shù)據(jù)庫。
1.2)分析需求轉(zhuǎn)為小的問題
①Xml字符串從哪里來,有沒有具體示例?
回答:通過HttpCilent調(diào)用接口,返回Xml字符串
②用什么方式解析Xml字符串?
回答:將xml字符串轉(zhuǎn)為json,再通過jsonObject獲取數(shù)據(jù)后轉(zhuǎn)為json字符串,將json字符串直接轉(zhuǎn)為對象,存入數(shù)據(jù)庫
③具體存入數(shù)據(jù)庫的是哪些字段?
回答:與實(shí)體類對應(yīng)的字段,根據(jù)具體業(yè)務(wù)決定
④按時(shí)存入的存入時(shí)間是?是以定時(shí)任務(wù)的方式嗎?如何實(shí)現(xiàn)定時(shí)任務(wù)
回答:以定時(shí)任務(wù)每天早晚八點(diǎn)
第二章、解決方案
2.1)使用的框架和技術(shù)
springboot,mybatis-plus,json,定時(shí)任務(wù)。文章來源:http://www.zghlxwxcb.cn/news/detail-798851.html
2.2)需求中每個(gè)小問題的解決
①調(diào)用接口,獲取Xml字符串
暫未寫好文章來源地址http://www.zghlxwxcb.cn/news/detail-798851.html
②Xml字符串解析為json,并獲得具體數(shù)據(jù)
public class XmlUtil {
public static String XmlToJson(String xmlString){
xmlString = "<?xml version=\\\"1.0\\\" ?>\n" +
"<RESULT>"+
"<CLAZZ>"+
"<STUDENT>"+
"<STUDENT_ID>test1</STUDENT_ID>\n" +
"<STUDENT_NAME>test</STUDENT_NAME>\n" +
"<STUDENT_AGE>test1</STUDENT_AGE>\n"+
"</STUDENT>"+
" <STUDENT>"+
" <STUDENT_ID>test2</STUDENT_ID>"+
" <STUDENT_NAME>test2</STUDENT_NAME>"+
" <STUDENT_AGE>test2</STUDENT_AGE>"+
"</STUDENT>"+
"</CLAZZ>"+
"</RESULT>" ;
JSONObject jsonObject = XML.toJSONObject(xmlString);
System.out.println(jsonObject.getJSONObject("RESULT").getJSONObject("CLAZZ").get("STUDENT").toString());
return jsonObject.getJSONObject("RESULT").getJSONObject("CLAZZ").getJSONArray("STUDENT").toString();
}
}
③數(shù)據(jù)庫存入字段
@Service
public class ChginfoServiceImpl implements ChginfoService {
@Autowired
StudentMapper studentMapper;
//接收json字符串,插入數(shù)據(jù)庫
public Integer insertChgData(String chgjson) {
//創(chuàng)建objectMapper對象
ObjectMapper objectMapper = new ObjectMapper();
//忽略字段設(shè)置
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
/* Student stu=null;
try {
//簡單json直接解析轉(zhuǎn)為對象,--》簡單的步驟
chg = objectMapper.readValue(chgjson, Student.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Integer num = studentMapper.insert(stu); */
Student stu=null;
List<Student> stuList = new ArrayList<>();
try {
//json數(shù)組解析轉(zhuǎn)為對象--》更復(fù)雜的步驟
//1、創(chuàng)建工廠對象
TypeFactory typeFactory = objectMapper.getTypeFactory();
//2、創(chuàng)建 具體實(shí)體類 的JavaType類型
JavaType jsonPersonType = typeFactory.constructType(Student.class);
//將jsonPersonType轉(zhuǎn)化成List<JsonPerson>類型,則也就是要?jiǎng)?chuàng)建List<>內(nèi)有參數(shù)的JavaType類型
// 即 constructParametricType(List.class, jsonPersonType)---> List<JsonPerson>
JavaType javaType = typeFactory.constructParametricType(List.class, jsonPersonType);
//將傳入的chgjson字符串,轉(zhuǎn)為List<Student>對象
stuList = objectMapper.readValue(chgjson, javaType);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//遍歷存入數(shù)據(jù)
for(Student s: stuList){
studentMapper.insert(s);
}
return 1;
}
}
④定時(shí)任務(wù)實(shí)現(xiàn)
到了這里,關(guān)于解析XML字符串并存入數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!