文章目录
- 启动添加参数
-Djdk.internal.lambda.dumpProxyClasses
就会在运行源码目录输出 class 文件
package manageSystemOfStudentInfo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class ManageXMLFile {
public static int getMaxId(String url) {
int max = 0;
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element e : list) {
int id = Integer.parseInt(e.attributeValue("id"));
if (id > max) {
max = id;
}
}
return max;
}
public static boolean queryAccount(String url, String username,
String password) {
boolean b = false;
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element element : list) {
if (String.valueOf(username).equals(
element.attributeValue("username"))
&& String.valueOf(password).equals(
element.attributeValue("password"))) {
b = true;
}
}
return b;
}
public static Document getDocument(String url) {
SAXReader reader = new SAXReader();
Document dom = null;
try {
dom = reader.read(new File(url));
} catch (DocumentException e) {
e.printStackTrace();
}
return dom;
}
public static void writerXML(String url, Document dom) throws IOException {
OutputFormat format = OutputFormat.createCompactFormat();
format.setIndent(" ");
format.setNewlines(true);
format.setEncoding("gbk");
XMLWriter xml = new XMLWriter(new FileOutputStream(url), format);
xml.write(dom);
}
public static void creatNullXmlFile(String url, String rootElement) {
Document dom = DocumentHelper.createDocument();
Element person = dom.addElement(rootElement);
dom.setRootElement(person);
try {
writerXML(url, dom);
} catch (IOException e) {
e.printStackTrace();
System.out.println("“创建空XML文件方法”中的异常");
}
}
public static void add(String url, String account[]) {
Document dom = getDocument(url);
Element person = dom.getRootElement();
Element act = person.addElement("account");
act.addAttribute("username", account[0]);
act.addAttribute("password", account[1]);
try {
writerXML(url, dom);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void add(String url, Student st) {
Document dom = getDocument(url);
Element person = dom.getRootElement();
Element stu = person.addElement("student");
stu.addAttribute("id", String.valueOf(getMaxId(url) + 1));
Element name = stu.addElement("name");
name.setText(st.getName());
Element sex = stu.addElement("sex");
sex.setText(st.getSex());
Element address = stu.addElement("address");
address.setText(st.getAddress());
Element age = stu.addElement("age");
age.setText(String.valueOf(st.getAge()));
Element grade = stu.addElement("grade");
grade.setText(st.getGrade());
Element email = stu.addElement("email");
email.setText(st.getEmail());
Element phone = stu.addElement("phone");
phone.setText(st.getPhone());
Element school = stu.addElement("school");
school.setText(st.getSchool());
try {
writerXML(url, dom);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void add(String url, Student st, int id) {
Document dom = getDocument(url);
Element person = dom.getRootElement();
Element stu = person.addElement("student");
stu.addAttribute("id", String.valueOf(id));
Element name = stu.addElement("name");
name.setText(st.getName());
Element address = stu.addElement("address");
address.setText(st.getAddress());
Element grade = stu.addElement("grade");
grade.setText(st.getGrade());
Element sex = stu.addElement("sex");
sex.setText(st.getSex());
Element school = stu.addElement("school");
school.setText(st.getSchool());
Element email = stu.addElement("email");
email.setText(st.getEmail());
Element phone = stu.addElement("phone");
phone.setText(st.getPhone());
Element age = stu.addElement("age");
age.setText(String.valueOf(st.getAge()));
try {
writerXML(url, dom);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int del(String url, int id) {
int flag = 0;
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element element : list) {
if (String.valueOf(id).equals(element.attributeValue("id"))) {
person.remove(element);
flag++;
}
}
try {
if (1 == flag) {
System.out.println("确认删除ID=" + id + "的学生信息(y或Y确认,其他取消删除操作)");
Scanner sc = new Scanner(System.in);
if (sc.next().equalsIgnoreCase("Y")) {
writerXML(url, dom);
flag++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (2 == flag) {
System.out.println("删除ID=" + id + "的学生信息成功……");
} else if (1 == flag) {
System.out.println("取消删除操作……");
} else {
System.out.println("没有ID=" + id + "的学生, 无法完成删除操作……");
}
return flag;
}
public static Students inputStud(Students s) {
Students stu = s;
Scanner sc = new Scanner(System.in);
System.out.println("ID=" + s.getId() + "学生的 Address 属性修改前的值为:"
+ s.getAddress() + "\n请输入新值:");
stu.setAddress(sc.next());
while (true) {
System.out.println("ID=" + s.getId() + "学生的 Age 属性修改前的值为:"
+ s.getAge() + "\n请输入新值:");
String str3 = sc.next();
if (ManageStudentInfo.regex("^([8-9]|((1|2|3|4)[0-9])|50)*", str3)) {
stu.setAge(Integer.parseInt(str3));
break;
} else {
System.out.println("年龄太大或太小,请输入8-50");
}
}
while (true) {
System.out.println("ID=" + s.getId() + "学生的 Grade 属性修改前的值为:"
+ s.getGrade() + "\n请输入新值(只能输入'初级'、'中级'、'高级'):");
String str3 = sc.next();
if (str3.equals("初级") || str3.equals("中级") || str3.equals("高级")) {
stu.setGrade(str3);
break;
} else {
System.out.println("输入错误,请重新输入。只能输入'初级'、'中级'、'高级':");
}
}
System.out.println("ID=" + s.getId() + "学生的 Email 属性修改前的值为:"
+ s.getEmail() + "\n请输入新值: ");
stu.setEmail(sc.next());
while (true) {
System.out.println("ID=" + s.getId() + "学生的 Phone 属性修改前的值为:"
+ s.getPhone() + "\n请输入新值: ");
String str3 = sc.next();
if (ManageStudentInfo.regex("^[1][358]\\d{9}$", str3)) {
stu.setPhone(str3);
break;
} else {
System.out.println("请输入 有效的11位电话号码");
}
}
System.out.println("ID=" + s.getId() + "学生的 School 属性修改前的值为:"
+ s.getSchool() + "\n请输入新值: ");
;
stu.setSchool(sc.next());
return stu;
}
public static void edit(String url, Students st) {
int flag = 0;
try {
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element element : list) {
if (String.valueOf(st.getId()).equals(
element.attributeValue("id"))) {
flag++;
st = inputStud(st);
element.element("address").setText(st.getAddress());
element.element("grade").setText(st.getGrade());
element.element("school").setText(st.getSchool());
element.element("email").setText(st.getEmail());
element.element("phone").setText(st.getPhone());
element.element("age").setText(String.valueOf(st.getAge()));
writerXML(url, dom);
flag++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (2 == flag)
System.out.println("修改ID=" + st.getId() + "的学生信息成功……");
else if (flag == 0)
System.out.println("ID=" + st.getId() + "不存在");
}
public static void edit(String url, Student st, String attribute) {
int flag = 0;
try {
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element element : list) {
if (String.valueOf(st.getId()).equals(
element.attributeValue("id"))) {
flag++;
if (attribute.equals("age")) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("ID=" + st.getId() + "学生的"
+ attribute + "属性,修改前的值为:"
+ element.elementText(attribute)
+ "\n请输入新值:");
String str3 = sc.next();
if (ManageStudentInfo.regex(
"^([8-9]|((1|2|3|4)[0-9])|50)*", str3)) {
element.element("age").setText(str3);
break;
} else {
System.out.println("年龄太大或太小,请输入8-50");
}
}
} else {
Scanner sc = new Scanner(System.in);
if (attribute.equals("phone")) {
while (true) {
System.out.println("ID=" + st.getId()
+ "学生的 Phone 属性修改前的值为:"
+ element.elementText(attribute)
+ "\n请输入新值: ");
String str3 = sc.next();
if (ManageStudentInfo.regex("^[1][358]\\d{9}$",
str3)) {
element.element("phone").setText(str3);
break;
} else {
System.out.println("请输入 有效的11位电话号码");
}
}
} else if (attribute.equals("grade")) {
while (true) {
System.out.println("ID=" + st.getId()
+ "学生的 Grade 属性修改前的值为:"
+ element.elementText(attribute)
+ "\n请输入新值(只能输入'初级'、'中级'、'高级'):");
String str3 = sc.next();
if (str3.equals("初级") || str3.equals("中级")
|| str3.equals("高级")) {
element.element("grade").setText(str3);
break;
} else {
System.out
.println("输入错误,请重新输入。只能输入'初级'、'中级'、'高级':");
}
}
} else {
System.out.println("id为" + st.getId() + "学生的"
+ attribute + "属性修改前的值为:"
+ element.elementText(attribute)
+ "\n请输入新值:");
element.element(attribute).setText(sc.next());
}
}
writerXML(url, dom);
flag++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (2 == flag)
System.out.println("修改ID=" + st.getId() + "的学生信息成功……");
else if (flag == 0)
System.out.println("ID=" + st.getId() + "不存在");
}
public static Students queryById(String url, int id) {
Document dom;
Students stuD = new Students();
dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
int flag = 0;
for (Element element : list) {
if (String.valueOf(id).equals(element.attributeValue("id"))) {
flag = 1;
stuD.setId(Integer.parseInt(element.attributeValue("id")));
stuD.setAge(Integer.parseInt(element.elementText("age")));
stuD.setName(element.elementText("name"));
stuD.setSex(element.elementText("sex"));
stuD.setAddress(element.elementText("address"));
stuD.setGrade(element.elementText("grade"));
stuD.setEmail(element.elementText("email"));
stuD.setPhone(element.elementText("phone"));
stuD.setSchool(element.elementText("school"));
}
}
if (flag == 0) {
System.out.println("没有ID=" + id + "的学生, 请重新输入……");
return null;
}
return stuD;
}
public static List<Students> queryAll(String url) {
List<Students> list = null;
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> listErgodic = person.elements();
List<Students> listSave = new ArrayList<Students>();
for (Element element : listErgodic) {
Students stuD = new Students();
stuD.setId(Integer.parseInt(element.attributeValue("id")));
stuD.setAge(Integer.parseInt(element.elementText("age")));
stuD.setName(element.elementText("name"));
stuD.setSex(element.elementText("sex"));
stuD.setAddress(element.elementText("address"));
stuD.setGrade(element.elementText("grade"));
stuD.setEmail(element.elementText("email"));
stuD.setPhone(element.elementText("phone"));
stuD.setSchool(element.elementText("school"));
listSave.add(stuD);
}
list = listSave;
return list;
}
public static int queryId(String url, int id) {
int flag = 0;
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
for (Element element : list) {
if (String.valueOf(id).equals(element.attributeValue("id"))) {
flag = 1;
}
}
return flag;
}
public static void queryId(String url) {
int flag = -1;
int arrId[] = new int[1];
List<int[]> lit = new ArrayList<int[]>();
Document dom = getDocument(url);
Element person = dom.getRootElement();
List<Element> list = person.elements();
System.out.print("不能添加的ID=");
int i = 1, j = 1;
for (Element element : list) {
if (1 == i) {
for (Element e : list) {
i++;
}
}
j++;
if (j != i) {
System.out.print(element.attributeValue("id") + ", ");
} else {
System.out.println(element.attributeValue("id"));
}
}
System.out.print("建议添加的ID=");
outerFor: for (int n = 1; n < getMaxId(url); n++) {
int k = 0;
for (Element e : list) {
k = Integer.parseInt(e.attributeValue("id"));
if (k == n) {
flag = 1;
continue outerFor;
}
}
System.out.print(n + ", ");
}
if (-1 == flag) {
System.out.println("建议输入ID= " + (getMaxId(url) + 1));
} else {
System.out.println((getMaxId(url) + 1) + "及更大值");
}
}
}