java解析xml并导入数据库(dom4j)_第1页
java解析xml并导入数据库(dom4j)_第2页
java解析xml并导入数据库(dom4j)_第3页
java解析xml并导入数据库(dom4j)_第4页
java解析xml并导入数据库(dom4j)_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java解析xml并导入数据库(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导入数据库成功!“);

}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〃也可,本文中为数据词典

for(inti=0;i

<personList.getLengthO;i++)〃节点从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

EMAIL

*/

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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论