版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
java解析xml并導(dǎo)入數(shù)據(jù)庫(dom4j)
importjava.io.File;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.util.Iterator;
importjava.util.List;
importorg.dom4j.Document;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;
publicclassTestXMLImport{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Stringsql="insertintoT_XML(NUMERO,REPOSICION,NOMBRE,
TURNOS)values(?,?,?,?)〃;
Connectionconn=null;
PreparedStatementpstmt=null;
try(
conn=DbUtil.getConnection();
pstmt=conn.prepareStatement(sql);
Documentdoc=newSAXReader().read(new
File(,ZD:/share/JavaProjects/drp/testxmlImport/xml/testOl.XML
〃));
ListitemList=doc.selectNodes(/,/ACCESOS/item/SOCIO/,);
for(Iteratoriter=itemList.iteratorO;iter.hasNext();)
(
Elementel=(Element)iter,next();
Stringnumero=el.elementText("NUMERO");
Stringreposicion=el.elementText("REPOSICION");
Stringnombre=el.elementText("NOMBRE");
ListturnosList=el.elements("TURNOS");
StringBuffersbString=newStringBuffer();
for(Iteratoriterl=turnosList.iteratorO;
iterl.hasNext();){
ElementturnosElt=(Element)iterl.next();
Stringlu=turnosElt.elementText("LU");
Stringma=turnosElt.elementText("MA");
Stringmi=turnosElt.elementText("MI");
Stringju=turnosElt.elementText("JU");
Stringvi=turnosElt.elementText(/,VI,/);
Stringsa=turnosElt.elementText("SA");
Stringdoo=turnosElt.elementText("DO");
sbString.append(lu+”,〃+ma+〃,“+mi+〃,〃+ju+
+vi++sa+":'+doo);
pstmt.setString(l,numero);
pstmt.setString(2,reposicion);
pstmt.setString(3,nombre);
pstmt.setString(4,sbString.toStringO)
pstmt.addBatch();
pstmt.executeBatch();
System.out.printin("將XML導(dǎo)入數(shù)據(jù)庫成功!“);
}catch(Exceptione){
e.printStackTrace();
}finally{
DbUtil.close(pstmt);
DbUtil.close(conn);
<?xmlversion="l.0"encoding="utf-8”?>
<ACCES0S>
<item>
<S0CI0>
<NUMER0>00045050</NUMER0>
<REP0SICI0N>0</REP0SICI0N>
<NOMBRE>MOISESM0REN0</N0MBRE>
<TURN0S>
<LU>TK/LU>
<MA>T2</MA>
<MI>T3</MI>
<JU>T4</JU>
<VI>T5</VI>
<SA>T6</SA>
<D0>T7</D0>
</TURN0S>
</S0CI0>
</item>
<item>
<SOCIO>
<NUMER0>00045051</NUMERO>
<REPOSICION>O</REPOSICION>
<NOMBRE>RUTHPENA</NOMBRE>
<TURNOSXLU>S1</LU><MA>S2</MA><MI>S3</MI><JU>S4</JUXVI
>S5</VIXSA>S6</SAXD0>S7</D0>
</TURNOS>
</SOCIO>
</item>
</ACCESOS>
1、
packagemyxml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.apache,crimson,tree.*;
importorg.xml.sax.SAXException;
importjava.io.
publicclassDomParserDemo(
privateDocumentdoc;
publicDomParserDemo0throwsException{
DocumentBui1derFactoryfactory=DocumentBui1derFac
tory.newInstanceO;
DocumentBuilderbuilder=factory.newDocumentBui1de
r();
Stringsource=
〃e:/jhb1117/classes/xmldoc/candidate,xml〃;
doc=builder.parse(source);
)
publicvoidshowDocument(){
//getall<person>
NodeListpersonList=doc.getElementsByTagName(XMLT
agDir.NODE_PERSON);//"PERSON〃也可,本文中為數(shù)據(jù)詞典
for(inti=0;i
<personList.getLengthO;i++)〃節(jié)點(diǎn)從0開始
(
Elementperson=(Element)personList.item(
i);
System,out.print(XMLTagDir.N0DE_NAME+
System,out.printin(getNodeValue(person,XMLT
agDir.NODE_NAME));
System,out.print(XMLTagDir.NODEADDRESS+
System,out.printIn(getNodeValue(person,XMLT
agDir.NODE_ADDRESS));
System.out.print(XMLTagDir.N0DE_TEL+”:〃);
System,out.printin(getNodeValue(person,XMLT
agDir.NODE_TEL));
System,out.print(XMLTagDir.N0DE_FAX+
System,out.printIn(getNodeValue(person,XMLT
agDir.NODE_FAX));
System,out.print(XMLTagDir.NODEEMAIL+〃:");
System,out.printin(getNodeValue(person,XMLT
agDir.N0DE_EMAIL));
System.out.printin();
}
publicStringgetNodeValue(Elementperson,StringnodeName)
(
NodeListnameList=person.getElementsByTagName(nodeName);
Elementname=(Element)nameList.item(O);
Texttext=(Text)name.getFirstChildO;
Stringvalue=text.getNodeValue();
returnvalue;
}
publicvoidsaveDocument(Stringpath)throwslOExceptio
n
(
FileWriterfw=newFileWriter(path);
XmlDocumentxmldoc=(XmlDocument)doc;
xmldoc.write(fw);
fw.close();
)
publicstaticvoidmain(Stringargs口){
try{
DomParserDemodoc=newDomParserDemo();
doc.showDocument();
//Stringpath=
^er/houjie/JavaAdvance/dist/xmldoc/parseOut.xml〃;
Stringpath=
,ze:/jhb1117/classes/xmldoc/jhbparseOut.xml〃;
doc.saveDocument(path);
System,out.print("filesaved〃);
}catch(Exceptione){
e.printStackTrace();
}
)
}
2、
packagemyxml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.apache,crimson,tree.*;
importjava.io.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2002</p>
*<p>Company:</p>
*?authorxxy
*?version1.0
*/
publicclassDomCreateDemo{
privateDocumentdoc;
publicDomCreateDemo()throwsException(
DocumentBui1derFactoryfactory=DocumentBuilderFactory.newl
nstanceO;
DocumentBuiIderbuilder二factory.newDocumentBuiIder();
doc二builder.newDocument();
)
publicvoidcreateDocument(){
if(doc==nul1)return;
ElementpeopleElement=doc.createElement(XMLTagDir
.NODE_PEOPLE);
for(inti=l;i<=3;i++){
ElementpersonElement=doc.createElement(
XMLTagDir.NODE_PERSON);
personElement.setAttribute(,ZPERSONID","E
〃+i);
//onepersonincludeseveralta
gs
Texttext=null;
ElementnameElement=doc.createElement(XM
LTagDir.NODE_NAME);
text=doc.createTextNode(,zmyName"+i);
nameElement.appendChiId(text);
personElement.appendChild(nameElement);
ElementaddressElement=doc.createElement
(XMLTagDir.NODEADDRESS);
text=doc.createTextNode("myAddress"+i);
addressElement.appendChild(text);
personElement.appendChiId(addressElement);
ElementtelElement=doc.createElement(XML
TagDir.NODE_TEL);
text=doc.createTextNode("myTel〃+i);
telElement.appendChild(text);
personElement.appendChild(telElement);
ElementfaxElement=doc.createElement(XML
TagDir.NODE_FAX);
text=doc.createTextNode(〃myFax〃+i);
faxElement.appendChild(text);
personElement.appendChild(faxElement);
ElementemailElement=doc.createElement(X
MLTagDir.NODE_EMAIL);
text=doc.createTextNode("myEmail"+i);
emailElement.appendChild(text);
personElement.appendChild(emailElement);
peopleElement.appendChild(personElement);
)
doc.appendChild(peopleElement);
)
publicvoidsaveDocument(Stringpath)throwslOExceptio
n(
FileWriterfout=newFileWriter(path);
XmlDocumentxmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
)
publicstaticvoidmain(String[]args)
try(
DomCreateDemodoc二=newDomCreateDemo();
doc.createDocument();
System.out.print(〃doccreated〃);
Stringpath二
:/jhb1117/classes/xmldoc/jhbcreateOut.xml〃;
//Stringpath=
,z
e:/houjie/JavaAdvance/dist/xmldoc/createOut.xml〃;
doc.saveDocument(path);
System.out.print("filesaved〃);
}catch(Exceptione){
e.printStackTrace();
)
)
)
3、
packagemyxml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.apache,crimson,tree.*;
importjava.io.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2002</p>
*<p>Company:</p>
*?authorxxy
*?version1.0
*/
publicclassDomCreateDemo(
privateDocumentdoc;
publicDomCreateDemo()throwsException(
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newl
nstance();
DocumentBuilderbuiIder二factory.newDocumentBuiIder();
doc=builder.newDocument();
)
publicvoidcreateDocument(){
if(doc==null)return;
ElementpeopleElement=doc.createElement(XMLTagDir
.NODE_PEOPLE);
for(inti=l;i<=3;i++){
ElementpersonElement=doc.createElement(
XMLTagDir.NODEPERSON);
personElement.setAttribute(Z,PERSONID〃,〃E
"+i);
peopleElement.appendChild(personElement);
)
doc.appendChild(peopleElement);
)
publicvoidsaveDocument(Stringpath)throwslOExceptio
FileWriterfout=newFileWriter(path);
XmlDocumentxmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
)
publicstaticvoidmain(String[]args)
try(
DomCreateDemodoc=newDomCreateDemo();
doc.createDocument();
System.out.print(〃doccreated〃);
//Stringpath=
^e^houjie/JavaAdvance/dist/xmldoc/createOut.xml〃
Stringpath=
:/jhb1117/classes/xmldoc/jhbcreateOut.xml〃;
doc.saveDocument(path);
System.out.print("filesaved〃);
}catch(Exceptione){
e.printStackTraceO;
}
)
}
4、
packagemyxml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.apache,crimson,tree.*;
importorg.xml.sax.SAXException;
importjava.io.
publicclassDomParserDemo(
privateDocumentdoc;
publicDomParserDemo()throws
lOException,ParserConfigurationException,SAXException{
DocumentBuiIderFactoryfactory=DocumentBuiIderFac
tory.newInstanceO;
DocumentBuiIderbuilder=factory.newDocumentBuiIde
r();
//Documentdoc=builder.parse(^resources/xmldoc/
candidate,xml〃);
//Stringsource=
〃f:/houjie/JavaAdvance/dist/xmldoc/candidate,xml〃;
Stringsource=
^ei/jhbl117/classes/xmldoc/candidate.xml〃;
doc=builder.parse(source);
)
publicvoidshowDocument(){
//getall<person>
NodeListpersonList=doc.getElementsByTagName(XMLT
agDir.NODE_PERSON);
for(inti=0;i<personList.getLengthO;i++){
Elementperson=(Element)personList.item(
i);
System,out.print(XMLTagDir.NODE_NAME);
System.out.printin(getNodeValue(person,XMLT
agDir.N0DE_NAME));
System,out.print(XMLTagDir.NODE_ADDRESS);
System,out.printIn(getNodeValue(person,XMLT
agDir.NODE_ADDRESS));
System,out.print(XMLTagDir.NODE_TEL);
System,out.printin(getNodeValue(person,XMLT
agDir.NODE_TEL));
System,out.print(XMLTagDir.NODE_FAX);
System.out.printin(getNodeValue(person,XMLT
agDir.N0DE_FAX));
System.out.print(XMLTagDir.NODE_EMAIL);
System.out.printin(getNodeValue(person,XMLT
agDir.NODE_EMAIL));
System,out.printin();
)
}
publicvoidshowAndSavePeop1eDocument(Documentdoc,String
path){
//getall<person>
NodeListpersonList=doc.getElementsByTagName(XMLT
agDir.NODE_PERSON);
for(inti=0;i<personList.getLengthO;i++){
Elementperson=(Element)personList.item(
i);
System,out.print(XMLTagDir.N0DE_NAME);
System,out.printin(getNodeValue(person,XMLT
agDir.N0DE_NAME));
System,out.print(XMLTagDir.NODEADDRESS);
System,out.printin(getNodeValue(person,XMLT
agDir.NODEADDRESS));
System,out.print.(XMLTagDir.NODETEL);
System,out.printin(getNodeValue(person,XMLT
agDir.NODETEL));
System.out.print(XMLTagDir.NODEFAX);
System,out.printin(getNodeValue(person,XMLT
agDir.NODE_FAX));
System,out.print(XMLTagDir.NODE_EMAIL);
System,out.printin(getNodeValue(person,XMLT
agDir.NODE_EMAIL));
System,out.printlnO;
)
try(
saveDocument(doc,path);
}catch(Exceptione){
e.printStackTrace();
)
)
publicStringgetNodeValue(Elementperson,StringnodeName)
NodeListnameList=person.getElementsByTagName(nodeName);
Elementname=(Element)nameList.item(O);
Texttext=(Text)name.getFirstChild();
Stringvalue=text.getNodeValue();
returnvalue;
)
publicvoidsaveDocument(Stringpath)throwslOExceptio
FileWriterfout=newFileWriter(path);
XmlDocumentxmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
)
publicvoidsaveDocument(Documentdoc,Stringpath)thro
wslOException{
FileWriterfout=newFileWriter(path);
XmlDocumentxmldoc二(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
)
publicstaticvoidmain(Stringargs口){
try{
DomParserDemodoc=newDomParserDemo()
doc.showDocument();
//Stringpath=
,ze:/houjie/JavaAdvance/dist/xmldoc/parseOut.xml〃;
Stringpath=
/ze:/jhb1117/classes/xmldoc/jhbparseOut.xml〃;
doc.saveDocument(path);
System,out.print("filesaved〃);
}catch(Exceptione){
e.printStackTrace();
)
)
}
5、
packagemyxml;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2002</p>
*<p>Company:</p>
*?authorxxy
*?version1.0
*/
/*
NAME
ADDRESS
TEL
FAX
*/
publicclassDBPeople(
publicDBPeopleO(
)
publicstaticfinalintNAME』;
publicstaticfinalintADI)RESS=2;
publicstaticfinalintTEL=3;
publicstaticfinalintFAX=4;
publicstaticfinalintEMAIL=5;
privateStringname;
privateStringaddress;
privateStringtel;
privateStringfax;
privateStringemail;
publicStringgetName0(
returnname;
)
publicvoidsetName(Stringname){
this,name=name;
)
publicvoidsetAddress(Stringaddress)
this,address=address;
)
publicStringgetAddressO{
returnaddress;
)
publicvoidsetTel(Stringtel){
this.tel=tel;
)
publicStringgetTel(){
returntel;
}
publicvoidsetFax(Stringfax){
this,fax=fax;
}
publicStringgetFax(){
returnfax;
)
publicvoidsetEmail(Stringemail)
this,email=email;
}
publicStringgetEmail(){
returnemail;
)
)
6、
packagemyxml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.apache,crimson,tree.*;
importjavax.xml.transform.*;
importjavax.xml.transform,dom.DOMSource;
importjavax.xml.transform,stream.StreamResult;
importjava.io.
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2002</p>
*<p>Company:</p>
*?authorxxy
*?version1.0
*/
publicclassDomTransform{
privateDocumentdoc;
privateTransformertransformer;
publicDomTransform()throwsException{
DocumentBui1derFactoryfactory=DocumentBuilderFactory.newl
nstance();
DocumentBuilderbuilder=factory.newDocumentBuiIder();
//Stringsource=
,ze:/houjie/JavaAdvance/dist/xmldoc/candidate,xml〃;
Stringsource=^er/jhbl117/classes/xmldoc/candidate.xml
〃
doc=builder,parse(source);
TransformerFactorytf=TransformerFactory.newInstanceO;
transformer=tf.newTransformer();
)
publicvoidchangeDocument(){
//getall<person>
NodeListpersonList=doc.getElementsByTagName(XMLTagDir.NOD
E_PERS0N);
for(inti=0;i<personList.getLengthO;i++){
Elementperson31(Element)personList.item(i);
setNodeValue(person,XMLTagDir.NODEADDRESS,
/znewAddress"+i);〃改變address值
System,out.print(XMLTagDir.NODEADDRESS);
System,out.printin(getNodeValue(person,XMLTagDir.NOD
E_ADDRESS));
}
)
publicvoidsetNodeValue(Elementperson,StringnodeName,St
ringnewValue){
NodeListnameList=person.getElementsByTagName(nodeName);
Elementname=(Element)nameList.item(O);
Texttext=(Text)name.getFirstChildO;
text.setNodeValue(newValue);
)
publicStringgetNodeValue(Elementperson,StringnodeName)
(
NodeListnameList=person.getElementsByTagName(nodeName);
Elementname=(Element)nameList.item(O);
Texttext=(Text)name.getFirstChildO;
Stringvalue=text.getNodeValue();
returnvalue;
)
publicvoidsaveDocument(Stringpath)throwslOExceptio
n,TransformerException{
FileWriterfout=newFileWriter(path);
DOMSourcesource=newDOMSource(doc);
StreamResultresult=newStreamResult(fout);
transformer,transform(source,result);
/*XmlDocumentxmldoc=(XmlDocument)doc;
xmldoc.write(fout);
*/fout.close();
)
publicstaticvoidmain(String[]args){
try(
DomTransformdoc=newDomTransformO;
doc.changeDocument();
System.out.println(〃docchanged〃);
//Stringpath=
^ei/houjie/JavaAdvance/dist/xmldoc/transformOut.xml〃;
Stringpath=
z,e:/jhb1117/classes/xmldoc/transformOut.xml〃;
doc.saveDocument(path);
System,out.print("filesaved〃);
}catch(Exceptione){
e.print
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度木制家具出口業(yè)務(wù)分包勞務(wù)合同3篇
- 體育中心2025年度灌溉系統(tǒng)專用化肥及農(nóng)藥供應(yīng)合同3篇
- 2025年度配電變壓器租賃與電網(wǎng)安全培訓(xùn)服務(wù)合同
- 二零二五年度新型民間借貸服務(wù)合同規(guī)范(2025版)
- 二零二五年度農(nóng)產(chǎn)品電商平臺(tái)入駐合同范本
- 二零二五年度民營(yíng)中小企業(yè)企業(yè)社會(huì)責(zé)任履行服務(wù)合同
- 二零二五年度工業(yè)廠房外墻鋁型板安裝與維護(hù)合同
- 二零二五年度美容美發(fā)店員工健康體檢服務(wù)合同2篇
- 二零二四年度新能源產(chǎn)業(yè)聯(lián)營(yíng)項(xiàng)目合同3篇
- 2025年水塘蓮藕種植承包與品牌推廣合作合同
- 南通市2025屆高三第一次調(diào)研測(cè)試(一模)地理試卷(含答案 )
- 2025年上海市閔行區(qū)中考數(shù)學(xué)一模試卷
- 2025中國人民保險(xiǎn)集團(tuán)校園招聘高頻重點(diǎn)提升(共500題)附帶答案詳解
- 重癥患者家屬溝通管理制度
- 法規(guī)解讀丨2024新版《突發(fā)事件應(yīng)對(duì)法》及其應(yīng)用案例
- IF鋼物理冶金原理與關(guān)鍵工藝技術(shù)1
- 銷售提成對(duì)賭協(xié)議書范本 3篇
- 勞務(wù)派遣招標(biāo)文件范本
- EPC項(xiàng)目階段劃分及工作結(jié)構(gòu)分解方案
- 《跨學(xué)科實(shí)踐活動(dòng)4 基于特定需求設(shè)計(jì)和制作簡(jiǎn)易供氧器》教學(xué)設(shè)計(jì)
- 信息安全意識(shí)培訓(xùn)課件
評(píng)論
0/150
提交評(píng)論