一、目標(biāo)
用xml和Xapth做一個(gè)簡單的數(shù)據(jù)管理系統(tǒng),對(duì)數(shù)據(jù)的進(jìn)行簡單的增、刪、改、查詢操作。如做一個(gè)簡單的班級(jí)學(xué)生信息管理系統(tǒng):
二、環(huán)境及開發(fā)工具
環(huán)境:Java
工具:EcliPSe
開發(fā)包:如圖
三、原理
dom4j是一個(gè)Java的XML API,類似于jdom,用來讀寫XML文件的。dom4j是一個(gè)非常非常優(yōu)秀的Java XML API,具有性能優(yōu)異、功能強(qiáng)大和極端易用使用的特點(diǎn),同時(shí)它也是一個(gè)開放源代碼的軟件,可以在SourceForge上找到它。
XPath 是一門在 XML 文檔中查找信息的語言。XPath 用于在 XML 文檔中通過元素和屬性進(jìn)行導(dǎo)航。XPath 使用路徑表達(dá)式在 XML 文檔中選取節(jié)點(diǎn)。節(jié)點(diǎn)是通過沿著路徑或者 step 來選取的。
用Xpath定位到相應(yīng)節(jié)點(diǎn),用dom4j進(jìn)行增、刪、改。
四,類分析,如下圖:
addframe:執(zhí)行添加操作時(shí)顯示的界面。
allfunction:對(duì)xml操作各功能的實(shí)現(xiàn)。
alterframe:執(zhí)行修改操作時(shí)顯示的界面。
deleteframe:執(zhí)行刪除操作時(shí)顯示的界面。
mainframe:主界面
queryframe:執(zhí)行查詢操作時(shí)顯示的界面。
五、主要實(shí)現(xiàn)如下:
import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.dom4j.xpath.*; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.*; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.xml.sax.SAXException; /** * @author ncw * create the function */ public class allfunction { /* * 獲取全部信息 */ public String getall(){ String allinformation=""; try { SAXReader saxReader = new SAXReader(); //saxReader.setEncoding("UTF-8"); Document document = saxReader.read(new java.io.File("manage.xml")); List academylist = document.selectNodes("/school/academy" ); for(int i=0;i<academylist.size();i++) { Element academyElement = (Element)academylist.get(i); allinformation=allinformation+academyElement.attribute("aid").getValue(); allinformation+=" 學(xué)院: "+"\n"; Iterator classiterator = academyElement.elementIterator("class"); while(classiterator.hasNext()) { Element classElement = (Element)classiterator.next(); allinformation+=classElement.attribute("cid").getValue(); allinformation+=" 班級(jí): "+"\n"; Iterator studentiterator = classElement.elementIterator("student"); while(studentiterator.hasNext()) { Element studentElement=(Element)studentiterator.next(); allinformation+="姓名:"; allinformation+=studentElement.elementText("name"); allinformation+=" 性別:"; allinformation+=studentElement.elementText("sex"); allinformation+=" 年齡:"; allinformation+=studentElement.elementText("age"); allinformation+=" 成績:"; allinformation+=studentElement.elementText("grade"); allinformation+="\n"; } } } }catch(Exception ex) { ex.printStackTrace(); } return allinformation; } /* * 獲得一個(gè)學(xué)生的信息 */ public String getperson(String name) { String person=""; try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new java.io.File("manage.xml")); List classlist = document.selectNodes("/school/academy/class" ); Iterator classiterator = classlist.iterator(); while(classiterator.hasNext()) { Element classElement = (Element)classiterator.next(); Iterator studentiterator = classElement.elementIterator("student"); while(studentiterator.hasNext()) { Element studentElement=(Element)studentiterator.next(); if(studentElement.elementText("name").equals(name)) { person+="學(xué)院:"+classElement.getParent().attributeValue("aid")+"\n"; person+="班級(jí):"+classElement.attributeValue("cid")+"\n"; person+="姓名:"+studentElement.elementText("name"); person+=" 性別:"+studentElement.elementText("sex"); person+=" 年齡:"+studentElement.elementText("age"); person+=" 成績:"+studentElement.elementText("grade"); } } } }catch(Exception ex) { ex.printStackTrace(); } return person; } /* * 刪除一個(gè)學(xué)生的信息 */ public boolean delete(String name) { boolean deleted=false; try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new java.io.File("manage.xml")); List classlist = document.selectNodes("/school/academy/class"); Iterator classiterator = classlist.iterator(); while(classiterator.hasNext()) { Element classElement = (Element)classiterator.next(); Iterator studentiterator = classElement.elementIterator("student"); while(studentiterator.hasNext()) { Element studentElement=(Element)studentiterator.next(); if(studentElement.elementText("name").equals(name)) { classElement.remove(studentElement); deleted=true; } } } try{ /** 格式化輸出,類型IE瀏覽一樣 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML編碼 */ format.setEncoding("GBK"); /** 將document中的內(nèi)容寫入文件中 */ // XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format); //保證編碼為UTF-8,支持中文寫入 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format); writer.write(document); writer.close(); }catch(Exception ex){ ex.printStackTrace(); } }catch(Exception ex) { ex.printStackTrace(); } return deleted; } /* * 修改一個(gè)學(xué)生的信息 */ public boolean alter(String aid,String cid,String name,String sex,String age,String grade) { boolean altered=false; try{ /* aid=new String(aid.getBytes("ISO8859-1"),"UTF-8"); cid=new String(cid.getBytes("ISO8859-1"),"UTF-8"); name=new String(name.getBytes("ISO8859-1"),"UTF-8"); sex=new String(sex.getBytes("ISO8859-1"),"UTF-8"); age=new String(age.getBytes("ISO8859-1"),"UTF-8"); grade=new String(grade.getBytes("ISO8859-1"),"UTF-8"); */ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new java.io.File("manage.xml")); List academylist = document.selectNodes("/school/academy" ); Iterator academyiterator = academylist.iterator(); while(academyiterator.hasNext()) { Element academyElement = (Element) academyiterator.next(); if(academyElement.attribute("aid").getValue().equals(aid)); Iterator classiterator = academyElement.elementIterator("class"); while(classiterator.hasNext()) { Element classElement = (Element)classiterator.next(); if(classElement.attribute("cid").getValue().equals(cid)); Iterator studentiterator = classElement.elementIterator("student"); while(studentiterator.hasNext()) { Element studentElement=(Element)studentiterator.next(); if(studentElement.elementText("name").equals(name)) { Element sexElement=studentElement.element("sex"); sexElement.setText(sex); Element ageElement=studentElement.element("age"); ageElement.setText(age); Element gradeElement=studentElement.element("grade"); gradeElement.setText(grade); altered=true; } } } } try{ /** 格式化輸出,類型IE瀏覽一樣 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML編碼 */ format.setEncoding("GBK"); /** 將document中的內(nèi)容寫入文件中 */ // XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format); //保證編碼為UTF-8,支持中文寫入 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format); writer.write(document); writer.close(); }catch(Exception ex){ ex.printStackTrace(); } }catch(Exception ex) { ex.printStackTrace(); } return altered; } /* * 添加一個(gè)學(xué)生的信息 */ public boolean add(String aid,String cid,String name,String sex,String age,String grade) { boolean add = false; try { /* aid=new String(aid.getBytes("ISO8859-1"),"UTF-8"); cid=new String(cid.getBytes("ISO8859-1"),"UTF-8"); name=new String(name.getBytes("ISO8859-1"),"UTF-8"); sex=new String(sex.getBytes("ISO8859-1"),"UTF-8"); age=new String(age.getBytes("ISO8859-1"),"UTF-8"); grade=new String(grade.getBytes("ISO8859-1"),"UTF-8"); */ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new java.io.File("manage.xml")); List academylist = document.selectNodes("/school/academy"); Iterator academyiterator = academylist.iterator(); while (academyiterator.hasNext()) { Element academyElement = (Element) academyiterator.next(); if (academyElement.attribute("aid").getValue().equals(aid)) { Iterator classiterator = academyElement.elementIterator("class"); while (classiterator.hasNext()) { Element classElement = (Element) classiterator.next(); if (classElement.attribute("cid").getValue().equals(cid)) { Element studentElement = classElement.addElement("student"); Element nameElement = studentElement.addElement("name"); nameElement.setText(name); Element sexElement = studentElement.addElement("sex"); sexElement.setText(sex); Element ageElement = studentElement.addElement("age"); ageElement.setText(age); Element gradeElement = studentElement.addElement("grade"); gradeElement.setText(grade); add = true; } } } } try{ /** 格式化輸出,類型IE瀏覽一樣 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML編碼 */ format.setEncoding("GBK"); /** 將document中的內(nèi)容寫入文件中 */ // XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format); //保證編碼為UTF-8,支持中文寫入 XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format); writer.write(document); writer.close(); }catch(Exception ex){ ex.printStackTrace(); } }catch(Exception ex) { ex.printStackTrace(); } return add; } }
alterframe deleteframe mainframe queryframe等類則實(shí)現(xiàn)相應(yīng)的界面顯示。
相應(yīng)xml結(jié)構(gòu)如下:
六:總結(jié):
1 功能簡單,操作不夠靈活
2 在對(duì)數(shù)據(jù)的讀入與存儲(chǔ)上有待改進(jìn)。