日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/20 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaEE之使用DOM4J和XPath对xml文档的添加,删除,查询操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序運行結果如下圖:



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(); //關閉流資源} }


總結

以上是生活随笔為你收集整理的JavaEE之使用DOM4J和XPath对xml文档的添加,删除,查询操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。