JavaEE之使用DOM4J和XPath对xml文档的添加,删除,查询操作

来源:互联网 发布:淘宝达人精品帖子 编辑:程序博客网 时间:2024/05/18 13:46

程序运行结果如下图:



XML文档如下:

<?xml version="1.0" encoding="UTF-8"?><exam>   <student idcard="444" examid="222">     <name>王五</name>      <location>沈阳</location>      <grade>78</grade>   </student>    <student idcard="333" examid="444">     <name>大东</name>      <location>青岛</location>      <grade>96</grade>   </student>  </exam>

代码如下:

package tom.project;/** * 使用DOM4J和XPath对xml文档的添加,删除,查询操作 */import java.io.FileOutputStream;import java.util.Scanner;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.junit.Test;public class NewTest {public static void main(String[] args) throws Exception {show();}public static void show() throws Exception{NewTest nt = new NewTest();System.out.println("添加用户:(a)  删除用户:(b)  查询成绩(c)");System.out.print("请输入操作类型:");Scanner sc = new Scanner(System.in);String check = sc.next();//从控制台过得操作数据if("a".equals(check)){nt.add(); }else if("b".equals(check)){nt.delete();}else if("c".equals(check)){nt.check(); }else{System.out.println("你输入的类型错误,请输入a或b或c");show();}}//添加学生信息@Testpublic  void add() throws Exception{//初始化变量String na="王五",address="佛山";String examId = "555",idCard= "444";String grade = "88";Scanner sc = new Scanner(System.in);System.out.print("请输入学生姓名:");na = sc.next();System.out.print("请输入学生准考证号:");examId = sc.next();System.out.print("请输入学生身份证号:");idCard = sc.next();System.out.print("请输入学生所在地:");address = sc.next();System.out.print("请输入学生成绩:");grade = sc.next();Document doc = read();Element root = doc.getRootElement();//获得根标签Element stu = root.addElement("student");//获取student标签stu.addAttribute("idcard", idCard);//添加idcard的属性idCardstu.addAttribute("examid", examId);Element name = stu.addElement("name");//添加name标签name.setText(na);//为name标签添加文本Element location = stu.addElement("location");location.setText(address);Element gra = stu.addElement("grade");gra.setText(grade); //写进文档write(doc);System.out.println("------添加数据成功---------");show();}//查询学生信息@Testpublic void check() throws Exception{System.out.print("请输入查询的学生准考证号:");Scanner sc = new Scanner(System.in);String examid = sc.next();SAXReader sr = new SAXReader(); Document doc = sr.read("./src/exam.xml");String xpath = "";xpath = "//student[@examid='"+examid+"']";Element el = (Element)doc.selectSingleNode(xpath);System.out.println(el.getName());//获取用户姓名Element name = el.element("name");System.out.print("姓名:" + name.getText()+",  ");//获取身份证号码String id = el.attributeValue("idcard");System.out.print("身份证号码:"+id+",  ");//获取准考证号码String ex = el.attributeValue("examid");System.out.print("准考证号码:"+ex+",  ");//获取地区Element location =el.element("location");System.out.print("地区:" +location.getText()+",  ");//获取成绩Element grade =el.element("grade");System.out.print("成绩:" + grade.getText());System.out.println();System.out.println("---------查询数据成功--------");System.out.print("是否返回主界面(y/n):");String y = sc.next();if("y".equalsIgnoreCase(y)){show();}elseSystem.out.println("好的,不返回,你喜欢逗留多久就多久,我不反对~");}//删除标签@Testpublic void delete() throws Exception{System.out.print("请输入要删除的学生姓名:");Scanner sc = new Scanner(System.in);String name = sc.next();Document doc = read(); Element root  = doc.getRootElement();String path = "";path = "//name[text()='"+name+"']";Element el = (Element)doc.selectSingleNode(path); System.out.println(el.getData());el.getParent().detach();//写进文档write(doc);System.out.println("---------已成功删除学生信息--------");System.out.print("是否返回主界面(y/n):");String y = sc.next();if("y".equalsIgnoreCase(y)){show();}elseSystem.out.println("好的,不返回,你喜欢逗留多久就多久,我不反对~");}//读取文档public Document read() throws Exception{SAXReader sr = new SAXReader(); Document doc = sr.read("./src/exam.xml");//根据路径读取xmlreturn doc;}//写进文档public void write(Document doc) throws Exception{XMLWriter writer = new XMLWriter(new FileOutputStream("./src/exam.xml"),OutputFormat.createPrettyPrint());writer.write(doc);//写进文档writer.close();//关闭流资源}}



1 0
原创粉丝点击