根據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包
重寫上邊的程式
package com.swift.kaoshi; import java.io.File; import java.lang.reflect.InvocationTargetException; 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); 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); } 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("繼續按學科查詢學員~~"); 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"); beanToObj(course, listStudent); } } 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"); beanToObj(course, listStudent); } } break; default: System.out.println("抱歉,您輸入的數字不符合要求,請重新輸入."); break; } break; case "2": System.out.println("繼續按性別查詢學員~~"); System.out.println("1.按男生 2.按女生"); String key_Sex = scan.nextLine(); switch (key_Sex) { case "1": System.out.println("繼續按男生查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element sex = stu.element("sex"); String sexText = sex.getText(); if (sexText.equals("男")) { beanToOneObj(course, stu); } } } break; case "2": System.out.println("繼續按JavaEE查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element sex = stu.element("sex"); String sexText = sex.getText(); if (sexText.equals("女")) { beanToOneObj(course, stu); } } } break; default: System.out.println("抱歉,您輸入的數字不符合要求,請重新輸入."); break; } break; case "3": System.out.println("繼續按年齡查詢學員~~"); System.out.println("1.按年齡20-25歲 2.按年齡26-35"); String key_age = scan.nextLine(); switch (key_age) { case "1": System.out.println("繼續按年齡查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element age = stu.element("age"); String ageText = age.getText(); if (Integer.parseInt(ageText)>=20&&Integer.parseInt(ageText)<=25) { beanToOneObj(course, stu); } } } break; case "2": System.out.println("繼續按年齡查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element age = stu.element("age"); String ageText = age.getText(); if (Integer.parseInt(ageText)>=26&&Integer.parseInt(ageText)<=35) { beanToOneObj(course, stu); } } } break; default: System.out.println("抱歉,您輸入的數字不符合要求,請重新輸入."); break; } break; case "4": System.out.println("繼續按分數查詢學員~~"); System.out.println("1.按分數60-80歲 2.按年齡80-100"); String key_score = scan.nextLine(); switch (key_score) { case "1": System.out.println("繼續按年齡查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element score = stu.element("score"); String scoreText = score.getText(); if (Integer.parseInt(scoreText)>=60&&Integer.parseInt(scoreText)<=80) { beanToOneObj(course, stu); } } } break; case "2": System.out.println("繼續按年齡查詢學員~~"); for (Element course : courses) { List<Element> studentsList = course.elements("student"); for (Element stu : studentsList) { Element score = stu.element("score"); String scoreText = score.getText(); if (Integer.parseInt(scoreText)>=81&&Integer.parseInt(scoreText)<=100) { beanToOneObj(course, stu); } } } break; default: System.out.println("抱歉,您輸入的數字不符合要求,請重新輸入."); break; } break; case "5": System.out.println("選擇結束,退出."); break a; default: System.out.println("抱歉,您輸入的數字不符合要求,請重新輸入."); break; } } } private static void beanToOneObj(Element course, Element stu) throws Exception { 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[] { course.attribute("name").getValue() }); // 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); } private static void beanToObj(Element course, List<Element> listStudent) throws Exception { for (Element stu : listStudent) { beanToOneObj(course, stu); } } }