【XML】XML解析与校验

时间:2023-01-09 15:14:57

Student

@XmlRootElement(name = "Root")
public class Student {
private String id;
private String name;
private String age;
private StdClass stdClass;

@XmlElement(name = "ID")
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
@XmlElement(name = "Name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
@XmlElement(name = "Age")
public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
@XmlElement(name = "StdClass")
public StdClass getStdClass() {
return stdClass;
}

public void setStdClass(StdClass stdClass) {
this.stdClass = stdClass;
}
}

StdClass

public class StdClass {
private String name;
private List<Teacher> teacherList;

@XmlAttribute
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
@XmlElementWrapper(name = "TeacherList")
@XmlElement(name = "Teacher")
public List<Teacher> getTeacherList() {
return teacherList;
}

public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
}

Teacher

public class Teacher {
private String name;

@XmlAttribute
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

main

public class TestService {

public static void main(String[] args) {
Student student = new Student();
student.setId(UUID.randomUUID().toString());
student.setAge("20");
student.setName("wzd");
StdClass stdClass = new StdClass();
stdClass.setName("一班");
student.setStdClass(stdClass);
ArrayList<Teacher> teachers = new ArrayList<>();
Teacher teacher = new Teacher();
teacher.setName("yxl");
teachers.add(teacher);
teachers.add(teacher);
stdClass.setTeacherList(teachers);
System.out.println(objToXmlStr(student));
}
/**
* xmlobj 转成 xml String
* @param object
* @return
*/
public static String objToXmlStr(Object object) {
String resStr = "";
try {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter stringWriter = new StringWriter();
marshaller.marshal(object, stringWriter);
resStr = stringWriter.toString();
stringWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
return resStr;
}
/**
* xml string 转 object
* @param clazz
* @param xmlStr
* @return
*/
public static Object XmlStrToObj(Class<?> clazz, String xmlStr) {
Object xmlObj = null;
Reader reader = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
reader = new StringReader(xmlStr);
xmlObj = unMarshaller.unmarshal(reader);
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return xmlObj;
}
}

results

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Age>20</Age>
<ID>0d6961c4-f88c-4598-abb4-92a8fbbfdb0b</ID>
<Name>wzd</Name>
<StdClass name="一班">
<TeacherList>
<Teacher>
<name>yxl</name>
</Teacher>
<Teacher>
<name>yxl</name>
</Teacher>
</TeacherList>
</StdClass>
</Root>

反射字段校验

/**
* 反射校验xml标签、属性
* @param bean
* @param str
*/
public static void checkObjField(Object bean, String str) {
try {
Class<?> clazz = bean.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
String name = field.getName();
Object o = field.get(bean);
Method method = clazz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
XmlElement xmlElement = method.getAnnotation(XmlElement.class);
if (xmlElement != null) {
if (!str.contains("<" + xmlElement.name()) && !"errorCode".equals(xmlElement.name())) {
System.out.println(xmlElement.name() + "标签错误");
}
}

XmlAttribute xmlAttribute = method.getAnnotation(XmlAttribute.class);
if (xmlAttribute != null) {
if (!str.contains(xmlAttribute.name() + "=")) {
System.out.println(xmlAttribute.name() + "属性错误");
}
}

if (o instanceof List) {
List<Object> list = (List) o;
for (Object a : list) {
checkObjField(a, str);
}
} else if (o instanceof String) {
checkObjField(o, str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}