2018.4.13 用java配置/生成Xml文件 结合IO流知识点
生活随笔
收集整理的這篇文章主要介紹了
2018.4.13 用java配置/生成Xml文件 结合IO流知识点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自己創建本地文件Hello.txt 里面有數據
小明/23/增城/廣東 小花/12/浦東/上海StudentManager.java
package com.lanqiao.dmeo7;import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List;import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter;public class StudentManager {// 創建集合List<Student> list = new ArrayList<Student>();// 加載數據方法public void load(String file) throws Exception {// 創建文件File files = new File("D:/Hello.txt");// 字節流轉字符流Reader re = new FileReader(files);BufferedReader br = new BufferedReader(re);String str = null;while ((str = br.readLine()) != null) {System.out.println("讀取文件:\n" + str);// 拆分每一行數據存進去數組String[] date = str.split("/");// 創建對象 將每一行的數據存進對象里面Student stu = new Student();stu.setName(date[0]);// 注意:在這里需要轉型 字符串轉整形stu.setAge(Integer.parseInt(date[1]));stu.setCity(date[2]);stu.setProvince(date[3]);// 將對象保存在集合中list.add(stu);}// 遍歷輸出集合中的數據 第一種for (int i = 0; i < list.size(); i++) {Student s = (Student) list.get(i);System.out.println(s.getName() + "\t" + s.getAge() + "\t" + s.getCity() + "\t" + s.getProvince() + " ");}// 第二種 foreach/** for(Student stu : list) { System.out.println(stu.getName()+" "+stu.getAge());* }*/}// 創建xml文件public void creatXml() throws FileNotFoundException, IOException {// 1.創建文檔Document doc = new Document();// 2.創建根結點 在這里的根結點是通過new 出來的 因為是從沒有到有的Element rootElement = new Element("Students");//通過集合循環添加對象里面的屬性 用setTest 方法for (int i = 0; i < list.size(); i++) {// 3.往根結點中添加下面的子節點rootElement.addContent(new Element("Student").addContent(new Element("name").setText(list.get(i).getName())) // 集合里面不指定范型是用不了Student// 對象里面的方法的// 這里要將整形轉化為字符串 只要在后面加一個雙引號就行了.addContent(new Element("age").setText(list.get(i).getAge() + "")).addContent(new Element("city").setText(list.get(i).getCity())).addContent(new Element("province").setText(list.get(i).getProvince())));}// 4.把根結點綁定在文檔上doc.addContent(rootElement);// 5.保存生成的Xml文檔IO流 這里是指定輸出格式 還有編碼格式XMLOutputter output = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));// 指定輸出xml的名字 用的是文件輸出流 OutputStream out = new FileOutputStream();// 讓子類去實現output.output(doc, new FileOutputStream("config.xml"));System.out.println("操作完成,請刷新");}// 增 追加xml文件內容public void addXml(Student stu) throws Exception {// 創建工廠SAXBuilder builder = new SAXBuilder();// 創建文檔Document doc = builder.build(new File("config.xml"));//在這里記得是new File 出來先// 獲取根結點Element rootEle = new Element("Students");// 往根結點下面的子節點中添加節點rootEle.addContent(new Element("Student").addContent(new Element("name").setText(stu.getName())).addContent(new Element("age").setText(stu.getAge() + "")).addContent(new Element("city").setText(stu.getCity())).addContent(new Element("province").setText(stu.getProvince())));// 將文檔綁定到根結點中doc.addContent(rootEle);XMLOutputter output = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));output.output(doc, new FileOutputStream("config.xml"));}// 刪 根據名字來刪除public void delXml(String delName) throws JDOMException, IOException {//創建工廠SAXBuilder builder = new SAXBuilder();//創建文檔Document doc = builder.build(new File("config.xml"));//獲取根結點Element ele = doc.getRootElement();//獲取Element 總根結點下面的所有子節點 List<Element> lis = ele.getChildren();//遍歷查找for (int i = 0; i < lis.size(); i++) {//獲取每一個具體的子節點 例如說 student student student Element e = lis.get(i);if(e.getName().equals(delName)) {//刪除子節點的時候要根據根結點來刪除ele.removeContent(e);System.out.println("Delete Name Successful");}}}// 改 根據名字來修改public void updateXml(String oldName, String newName) throws JDOMException, IOException {// 創建工廠SAXBuilder builder = new SAXBuilder();// 創建文檔Document doc = builder.build(new File("config.xml"));// 獲取根結點Element ele = doc.getRootElement();//獲取根結點下面的子節點 返回的是一個集合List<Element> li = ele.getChildren(); //記得指定集合的類型 不然下面需要轉類型//遍歷查找for(int i = 0; i<li.size();i++) {Element e = li.get(i);if(e.getChildText("name").equals(oldName)) {//getChile 和 getChileText 的區別是 前者獲取的是節點 后者是獲取節點里面的值e.getChild("name").setText(newName);//這句話的意思是 第二個根結點下獲取的name這個子節點 并且賦值成newNameSystem.out.println("Update Info Successfully!!");}}//將根結點綁定在文檔上面doc.addContent(ele);XMLOutputter output = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));output.output(doc, new FileOutputStream("config.xml"));}// 查 根據名字來查詢public void findByName(String findByName) throws JDOMException, IOException {//創建工廠SAXBuilder builder = new SAXBuilder();//創建文檔Document doc = builder.build(new File("config.xml"));//獲取根結點Element ele = doc.getRootElement();//將根結點下面的所有節點找出來 返回element 集合 而不是StudentList<Element> list = ele.getChildren();//遍歷查找for (int i = 0; i < list.size(); i++) {Element e = list.get(i);if(e.getName().equals(findByName)) {System.out.println("查詢結果如下:\n"+e.getChildText("age")+"\t"+e.getChildText("city"));}}}}Test.java
package com.lanqiao.dmeo7;/*** 對XMl的增、刪、改、查的綜合運用* * 結合集合的運用* @author qichunlin**/ public class Test {public static void main(String[] args) throws Exception {StudentManager sm = new StudentManager();sm.load("D:/Hello.txt");//增sm.addXml(new Student("小花",23,"武漢","湖南"));//刪sm.delXml("小齊");//改sm.updateXml("小齊","小qi");//查sm.findByName("小七");} }Student.java
package com.lanqiao.dmeo7;/*** 學生類 * 屬性: 名字 年齡 城市 省份* * @author qichunlin**/ public class Student {private String name;private int age;private String city;private String province;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 getCity() {return city;}public void setCity(String city) {this.city = city;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String toString() {return "Student [name=" + name + ", age=" + age + ", city=" + city + ", province=" + province + "]";}public Student(String name, int age, String city, String province) {super();this.name = name;this.age = age;this.city = city;this.province = province;}public Student() {super();// TODO Auto-generated constructor stub}}轉載于:https://www.cnblogs.com/qichunlin/p/8819612.html
總結
以上是生活随笔為你收集整理的2018.4.13 用java配置/生成Xml文件 结合IO流知识点的全部內容,希望文章能夠幫你解決所遇到的問題。

- 上一篇: 02.Python基础
- 下一篇: wenbao与windows命令