




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
大連海事大學(xué)本科生實驗報告《XML技術(shù)及其應(yīng)用》實驗報告院(系):交通運輸管理學(xué)院專業(yè)班級:電子商務(wù)2011級1班課程名稱:XML技術(shù)及其應(yīng)用姓名:周慧敏學(xué)號:2220113494指導(dǎo)教師:翟軍完成日期:2013年11
實驗:解析與生成XML文件實驗名稱:編程實現(xiàn)解析與生成XML文件的算法實驗?zāi)康模?.理解生成與解析XML文件的算法原理及其應(yīng)用。2.掌握該算法的程序?qū)崿F(xiàn)過程。3.學(xué)會使用該算法對XML文件進行解析和生成XML文件。4.知道如何將eclipse與access數(shù)據(jù)庫連接,學(xué)會運用SQL算法將數(shù)據(jù)導(dǎo)入和導(dǎo)出數(shù)據(jù)庫。實驗要求:通過Java編程實現(xiàn)對“通訊錄”的XML文檔的解析,并把解析結(jié)果存到數(shù)據(jù)庫的表中。進一步實現(xiàn)相反的過程,即將數(shù)據(jù)庫表的內(nèi)容讀出來,并將其轉(zhuǎn)化為XML文件存儲起來。四、實驗內(nèi)容與步驟:1.
算法流程圖:開始開始調(diào)用contact.xml文件并解析將解析后得到的數(shù)據(jù)存入與eclipse連接的access里名為db1的數(shù)據(jù)庫的contact表中導(dǎo)出存在access數(shù)據(jù)庫中的數(shù)據(jù)使用所得的數(shù)據(jù)生成新的contact1.xml文件結(jié)束2.實驗步驟:(1).調(diào)用contact.xml文件并解析try{ doc=builder.parse(newFile("contact.xml")); } catch(SAXExceptione){ e.printStackTrace(); } NodeListnlCurrent=elmtuser.getElementsByTagName("name"); name=nlCurrent.item(0).getFirstChild().getNodeValue(); System.out.println("name:"+nlCurrent.item(0).getFirstChild().getNodeValue()); u.setname(name); (2).將解析后得到的數(shù)據(jù)存入access數(shù)據(jù)庫里的contact表中try{ Connectioncon=null; Statementsql; Connect.Store(u); } catch(SQLExceptione){ e.printStackTrace(); }try { con=DriverManager.getConnection(url); Statementsta=con.createStatement(); sta.executeUpdate(String.format("insertintocontact(id,name,phone,address)values(%d,'%s','%s','%s')", u.getid(),u.getname(),u.getphone(),u.getaddress())); }(3).導(dǎo)出存在access數(shù)據(jù)庫中的數(shù)據(jù)try{ conn=DriverManager.getConnection(url); Statementsta=conn.createStatement(); ResultSetresult=sta.executeQuery(String.format("select*fromcontactwhereid=%d",id)); if(result.next()){ useru=newuser(); u.setname(result.getString("name")); u.setphone(result.getString("phone")); u.setaddress(result.getString("address")); returnu; } returnnull; }(4).使用所得的數(shù)據(jù)生成新的contact1.xml文件try{ @SuppressWarnings("resource") BufferedWriterchangetoXml=newBufferedWriter(newFileWriter(xmlName)); changetoXml.write("<?xmlversion=\"1.0\"encoding=\"gb2312\"?>"); changetoXml.newLine(); changetoXml.write("<contact>"); for(intid=1;id<3;id++){ useru=Connect.getInformation(id); changetoXml.newLine(); changetoXml.write("<user>"); changetoXml.newLine(); changetoXml.write("<name>"+u.getname()+"</name>"); changetoXml.newLine(); changetoXml.write("<phone>"+u.getphone()+"</phone>"); changetoXml.newLine(); changetoXml.write("<address>"+u.getaddress()+"</address>"); changetoXml.newLine(); changetoXml.write("</user>"); } changetoXml.newLine(); changetoXml.write("</contact>"); changetoXml.flush(); }五、實驗結(jié)果:1.contact.xml文件:2.解析contact.xml文件后的結(jié)果:3.數(shù)據(jù)存入access數(shù)據(jù)庫中的contact表之后:4.生成的新的xml文件contact1.xml:六、實驗總結(jié):通過本次實驗,了解并明白了解析與生成xml文件的算法,同時也掌握了連接數(shù)據(jù)庫的方法,并且學(xué)會了使用SQL語句。在編程的過程中,遇到不明白的地方都會向同學(xué)請教,或者尋求搜索引擎的幫助,很多問題都逐個地解決了。源代碼:
Control.javapackagecontact;publicclassControl{ publicstaticvoidmain(String[]args)throwsException{ System.out.println("解析contact的xml文件后的結(jié)果:"); DomParsedomparse=newDomParse(); domparse.Dom(); System.out.println("從access數(shù)據(jù)庫中調(diào)用數(shù)據(jù)后,生成的新的xml文件contact1:"); StringxmlName="contact1.xml"; GeneratethisClass=newGenerate(); thisClass.createXml(xmlName); }}Connect.javapackagecontact;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;publicclassConnect{ staticStringurl="jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb)};DBQ=db1.mdb"; publicstaticvoidStore(useru)throwsSQLException{ Connectioncon=null; try { con=DriverManager.getConnection(url); Statementsta=con.createStatement(); sta.executeUpdate(String.format("insertintocontact(id,name,phone,address)values(%d,'%s','%s','%s')", u.getid(),u.getname(),u.getphone(),u.getaddress())); } finally { con.close(); } } publicstaticusergetInformation(intid)throwsSQLException{ Connectionconn=null; try{ conn=DriverManager.getConnection(url); Statementsta=conn.createStatement(); ResultSetresult=sta.executeQuery(String.format("select*fromcontactwhereid=%d",id)); if(result.next()){ useru=newuser(); u.setname(result.getString("name")); u.setphone(result.getString("phone")); u.setaddress(result.getString("address")); returnu; } returnnull; }finally{ conn.close(); } }}DomParse.javapackagecontact;importjavax.xml.parsers.*;importjava.io.*;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;importorg.w3c.dom.*;importorg.xml.sax.SAXException;publicclassDomParse{ publicvoidDom(){ DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); DocumentBuilderbuilder=null; try{ builder=factory.newDocumentBuilder(); } catch(ParserConfigurationExceptione){ e.printStackTrace(); } Documentdoc=null; try{ doc=builder.parse(newFile("contact.xml")); } catch(SAXExceptione){ e.printStackTrace(); } catch(IOExceptione){ e.printStackTrace(); } Stringname,phone,address; Elementcontact=doc.getDocumentElement(); NodeListnodeList=contact.getElementsByTagName("user"); for(inti=0;i<nodeList.getLength();i++){ useru=newuser(); u.setid((i+1)); Elementelmtuser=(Element)nodeList.item(i); NodeListnlCurrent=elmtuser.getElementsByTagName("name"); name=nlCurrent.item(0).getFirstChild().getNodeValue(); System.out.println("name:"+nlCurrent.item(0).getFirstChild().getNodeValue()); u.setname(name); nlCurrent=elmtuser.getElementsByTagName("phone"); phone=nlCurrent.item(0).getFirstChild().getNodeValue(); System.out.println("phone:"+nlCurrent.item(0).getFirstChild().getNodeValue()); u.setphone(phone); nlCurrent=elmtuser.getElementsByTagName("address"); address=nlCurrent.item(0).getFirstChild().getNodeValue(); System.out.println("address:"+nlCurrent.item(0).getFirstChild().getNodeValue()); u.setaddress(address); try{ Connectioncon=null; Statementsql; Connect.Store(u); } catch(SQLExceptione){ e.printStackTrace(); } } } }user.javapackagecontact;publicclassuser{ intid;Stringname; Stringphone; Stringaddress; publicvoidsetid(intid){ this.id=id; } publicintgetid(){ returnthis.id; } publicvoidsetname(Stringname){ =name; } publicStringgetname(){ return; } publicvoidsetphone(Stringphone){ this.phone=phone; } publicStringgetphone(){ returnthis.phone; } publicvoidsetaddress(Stringaddress){ this.address=address; } publicStringgetaddress(){ returnthis.address; }}Generate.javapackagecontact;importjava.io.BufferedWriter;importjava.io.FileWriter;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassGenerate{StringxmlName=newString(); publicvoidcreateXml(Strings){ xmlName=s; try{ @SuppressWarnings("resource") BufferedWriterchangetoXml=newBufferedWriter(newFileWriter(xmlName));
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZZB 3589-2023 潔凈轉(zhuǎn)子泵標準
- 2025年度礦山新能源利用合作開發(fā)協(xié)議
- 二零二五年度船舶租賃與船舶融資租賃合同
- 二零二五年度金融產(chǎn)品創(chuàng)新增資協(xié)議
- 2025年度酒店品牌授權(quán)及加盟合作協(xié)議
- 二零二五年度有機果園使用權(quán)及品牌授權(quán)合同
- 二零二五美容院轉(zhuǎn)讓合同包含員工培訓(xùn)體系與職業(yè)發(fā)展規(guī)劃
- 2025年度旅游度假區(qū)合租商鋪合作協(xié)議
- 二零二五年度知識產(chǎn)權(quán)標準化與認證顧問合同
- 二零二五年度科技園區(qū)出租房承包管理協(xié)議
- 森林防火知識
- 2025年湖南鐵路科技職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫參考答案
- 2025年黑龍江林業(yè)職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫帶答案
- 《ISO 56000-2025創(chuàng)新管理 基礎(chǔ)和術(shù)語》之1:“引言+范圍+術(shù)語和定義”專業(yè)深度解讀與應(yīng)用指導(dǎo)材料(雷澤佳編寫2025A0)-1-150
- DB37-T4817-2025 瀝青路面就地冷再生技術(shù)規(guī)范
- 2025年公共營養(yǎng)師三級理論試題及答案
- 提高設(shè)備基礎(chǔ)預(yù)埋螺栓一次安裝合格率
- 煤礦防治水安全質(zhì)量標準化評分表
- 2024年科技節(jié)小學(xué)科普知識競賽題及答案(共100題)
- 2025年度教育培訓(xùn)機構(gòu)學(xué)生綜合素質(zhì)評價協(xié)議3篇
- 氧氣管道吹掃、打壓方案
評論
0/150
提交評論