Java Learning to import XML into a database

Source: Internet
Author: User

      We know there are many ways to Import XML into a database by inserting the number into the database.,most of the previous contacts were throughSQLthe statement simply inserts a single piece of data,to learn today is toXMLdata is added to the database at once:

the first thing to write is an XML file :

<?xml version= "1.0" encoding= "Utf-8"?><accesos> <item> <SOCIO> <numero>00 045050</numero> <REPOSICION>0</REPOSICION> <nombre>moises Moreno</nombre&gt           ; <TURNOS> <LU>T1</LU> <MA>T2</MA> &LT;MI&GT;T3&L t;/mi> <JU>T4</JU> <VI>T5</VI> <sa>t6</sa > <DO>T7</DO> </TURNOS> </SOCIO> </item> &LT;ITEM&G         T               <SOCIO> <NUMERO>00045051</NUMERO> <REPOSICION>0</REPOSICION>                    <nombre>ruth pena</nombre> <TURNOS> <LU>S1</LU> <MA>S2</MA> <MI>S3</MI> <ju>s4</ Ju> <VI>S5</VI> <SA>S6</SA> <do>s 7</do> </TURNOS> </SOCIO> </item></ACCESOS>

          and then write the ReadXMLmethod of the file:putdom4jof theJarpackage introduced toWeb-infounder theLiband add a reference:

Import org.dom4j.document;import org.dom4j.element;import org.dom4j.io.saxreader;public static void Main (String[] args) {//Insert data SQL statement string sql= "insert into T_xml (NUMERO, reposicion, Nombre, Turnos) VALUES (?,?,?,?)"; Connection Conn=null; PreparedStatement pstmt=null;try{conn=dbutil.getconnection ();p stmt=conn.preparestatement (SQL);//Read XML file document Doc=new Saxreader (). Read (New File ("f:/j2eemyself/drp/test_xmlimport/xml/test01. XML ");//Select the node of the XML file list itemlist=doc.selectnodes (" Accesos/item/socio ");//traverse the read out of the XML node for (Iterator iter=    Itemlist.iterator (); Iter.hasnext ();) {element el= (Element) Iter.next ();    Reads the node content String numero=el.elementtext ("numero"); String reposicion = El.elementtext ("reposicion"); String nombre = El.elementtext ("nombre");//traverse the contents of the Turnos node list turnoslist = El.elements ("Turnos"); StringBuffer sbstring=new StringBuffer (); for (Iterator Iter1=turnoslist.iterator (); Iter1.hasnext ();) {Element turnoselt= (Element) iter1.next (); String lu = Turnoselt.elementtext ("Lu"); String ma = turnoselt.elementtext ("Ma"); String mi = turnoselt.elementtext ("Mi"); String Ju = Turnoselt.elementtext ("Ju"); String VI = turnoselt.elementtext ("VI"); String sa = turnoselt.elementtext ("sa");  String doo = Turnoselt.elementtext ("Do"), Sbstring.append (Lu + "," + Ma + "," + mi + "," + Ju + "," + VI + "," + sa + "," + doo);} Assign a value for SQL statement pstmt.setstring (1, numero);p stmt.setstring (2, reposicion);p stmt.setstring (3, Nombre);p stmt.setstring (4,    Sbstring.tostring ());p stmt.addbatch ();    } pstmt.executebatch (); System.out.print ("Import XML to database successfully");} catch (Exception e) {e.printstacktrace ();} Finally{dbutil.close (pstmt);D butil.close (conn);}}

It 's so simple that you can put XML The data in the file is read into the database: the results in the database:

actually put XML the data in the database is simple to read, as long as you iterate through the data in each node.


Java Learning to import XML into a database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.