java XML 通过BeanUtils的population为对象赋值 根据用户选择进行dom4j解析
程序员文章站
2022-07-06 19:37:46
根据xml文件设计Student对象 设计学生类 根据菜单进行解析 使用jar包 ......
根据xml文件设计Student对象
<?xml version="1.0" encoding="UTF-8"?> <students> <course name = "平面设计"> <student id = "it001"> <name>章子怡</name> <age>20</age> <sex>女</sex> <score>99</score> </student> <student id = "it002"> <name>杨颖</name> <age>21</age> <sex>女</sex> <score>100</score> </student> </course> <course name = "JavaEE"> <student id = "it003"> <name>汪峰</name> <age>22</age> <sex>男</sex> <score>89</score> </student> <student id = "it004"> <name>撒贝宁</name> <age>23</age> <sex>男</sex> <score>90</score> </student> </course> </students>
设计学生类
package com.swift.kaoshi; public class Student { private String id; private String name; private int age; private String sex; private int score; private String course; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String id, String name, int age, String sex, int score, String course) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.score = score; this.course = course; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + ", course=" + course + "]"; } }
根据菜单进行解析
package com.swift.kaoshi; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import org.apache.commons.beanutils.BeanUtils; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /* 请按以下要求编写代码: 1. 按XML文档描述创建Student类(注意:包含id字段、科目字段); 2. 为用户列出菜单: 【查询学员】 1.按学科 2.按性别 3.按年龄 4.按分数 5.退出 3. 用户选择: 1. 按学科:检索出XML中所有的”学科”名称,并继续列出菜单: 例如:1.平面设计 2.JavaEE 2. 按性别:列出性别选择二级菜单: 例如:1.男 2.女 3. 按年龄:请用户输入年龄范围,格式:小值-大值。 例如:20-25 4. 按分数:请用户输入分数范围,格式:小值-大值。 例如:80-100 4. 根据用户输入,在XML文件中检索出相应的学员信息,并使用Student对象封装,多个Student对象封装到一个ArrayList中; 5. 遍历ArrayList,为用户显示查询结果;*/ public class ReflectStudent { public static void main(String[] args) throws Exception { inputQuery(); } private static void inputQuery() throws Exception { System.out.println("[查询学员]"); List<String> courseList=new ArrayList<String>(); Scanner scan = new Scanner(System.in); a:while (true) { System.out.println(); System.out.println("1.按学科 2.按性别 3.按年龄 4.按分数 5.退出"); String key=scan.nextLine(); switch (key) { case "1": System.out.println("继续按学科查询学员~~"); SAXReader sax=new SAXReader(); Document document = sax.read(new File("students.xml")); Element root =document.getRootElement(); List<Element> courses = root.elements("course"); for(Element course:courses) { Attribute attribute=course.attribute("name"); String courseValue=attribute.getValue(); // System.out.println(courseValue); courseList.add(courseValue); } System.out.println("1.按"+courseList.get(0)+" 2.按"+courseList.get(1)); String key_Course=scan.nextLine(); switch (key_Course) { case "1": System.out.println("继续按平面设计查询学员~~"); for(Element course:courses) { Attribute attribute=course.attribute("name"); String courseValue=attribute.getValue(); if(courseValue.equals("平面设计")) { // System.out.println("ok"); List<Element> listStudent=course.elements("student"); for(Element stu:listStudent) { String id=stu.attribute("id").getValue(); String name=stu.element("name").getText(); String age=stu.element("age").getText(); String sex=stu.element("sex").getText(); String score=stu.element("score").getText(); //反射得到对象,BeanUtils设置属性进对象 Class clazz=Class.forName("com.swift.kaoshi.Student"); Object obj=clazz.getConstructor().newInstance(); Map<String,String[]> stus=new HashMap<String,String[]>(); stus.put("id", new String[] {id}); stus.put("name", new String[] {name}); stus.put("age", new String[] {age}); stus.put("sex", new String[] {sex}); stus.put("score", new String[] {score}); stus.put("course", new String[] {courseValue}); // System.out.println(stus); Set<String> set = stus.keySet(); for(String s:set) { // System.out.println(s); // System.out.println(Arrays.toString(stus.get(s))); } BeanUtils.populate(obj, stus); System.out.println(obj); } } } break; case "2": System.out.println("继续按JavaEE查询学员~~"); for(Element course:courses) { Attribute attribute=course.attribute("name"); String courseValue=attribute.getValue(); if(courseValue.equals("JavaEE")) { // System.out.println("ok"); List<Element> listStudent=course.elements("student"); for(Element stu:listStudent) { String id=stu.attribute("id").getValue(); String name=stu.element("name").getText(); String age=stu.element("age").getText(); String sex=stu.element("sex").getText(); String score=stu.element("score").getText(); //反射得到对象,BeanUtils设置属性进对象 Class clazz=Class.forName("com.swift.kaoshi.Student"); Object obj=clazz.getConstructor().newInstance(); Map<String,String[]> stus=new HashMap<String,String[]>(); stus.put("id", new String[] {id}); stus.put("name", new String[] {name}); stus.put("age", new String[] {age}); stus.put("sex", new String[] {sex}); stus.put("score", new String[] {score}); stus.put("course", new String[] {courseValue}); // System.out.println(stus); Set<String> set = stus.keySet(); for(String s:set) { // System.out.println(s); // System.out.println(Arrays.toString(stus.get(s))); } BeanUtils.populate(obj, stus); System.out.println(obj); } } } break; default: System.out.println("抱歉,您输入的数字不符合要求,请重新输入."); break; } break ; case "2": System.out.println("继续按性别查询学员~~"); break ; case "3": System.out.println("继续按年龄查询学员~~"); break ; case "4": System.out.println("继续按分数查询学员~~"); break ; case "5": System.out.println("选择结束,退出."); break a; default: System.out.println("抱歉,您输入的数字不符合要求,请重新输入."); break; } } } }
使用jar包