【XML】XML解析与校验

2023-01-09 14:30:38 来源:51CTO博客


【资料图】

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 teacherList;    @XmlAttribute    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @XmlElementWrapper(name = "TeacherList")    @XmlElement(name = "Teacher")    public List getTeacherList() {        return teacherList;    }    public void setTeacherList(List 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 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

    20    0d6961c4-f88c-4598-abb4-92a8fbbfdb0b    wzd                                        yxl                                        yxl                        

反射字段校验

/**     * 反射校验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 list = (List) o;                    for (Object a : list) {                        checkObjField(a, str);                    }                } else if (o instanceof String) {                    checkObjField(o, str);                }            }        } catch (Exception e) {            e.printStackTrace();        }    }                
              
            
            

标签:

上一篇:最新消息:扫雷程序:基础版
下一篇:当前聚焦:应用性能监控系统SkyWalking