解析XML字符串,并将解析后得到的结果,按时存入数据库。
回答:通过HttpCilent调用接口,返回Xml字符串
回答:将xml字符串转为json,再通过jsonObject获取数据后转为json字符串,将json字符串直接转为对象,存入数据库
回答:与实体类对应的字段,根据具体业务决定
回答:以定时任务每天早晚八点
springboot,mybatis-plus,json,定时任务。
暂未写好
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();
}
}
@Service
public class ChginfoServiceImpl implements ChginfoService {
@Autowired
StudentMapper studentMapper;
//接收json字符串,插入数据库
public Integer insertChgData(String chgjson) {
//创建objectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
//忽略字段设置
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
/* Student stu=null;
try {
//简单json直接解析转为对象,--》简单的步骤
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数组解析转为对象--》更复杂的步骤
//1、创建工厂对象
TypeFactory typeFactory = objectMapper.getTypeFactory();
//2、创建 具体实体类 的JavaType类型
JavaType jsonPersonType = typeFactory.constructType(Student.class);
//将jsonPersonType转化成List<JsonPerson>类型,则也就是要创建List<>内有参数的JavaType类型
// 即 constructParametricType(List.class, jsonPersonType)---> List<JsonPerson>
JavaType javaType = typeFactory.constructParametricType(List.class, jsonPersonType);
//将传入的chgjson字符串,转为List<Student>对象
stuList = objectMapper.readValue(chgjson, javaType);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//遍历存入数据
for(Student s: stuList){
studentMapper.insert(s);
}
return 1;
}
}