java 解析xml 并导入数据库(dom4j )
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestXMLImport {
/**
* @param args
*/
public static void main(String[] args) {
String sql = "insert into T_XML(NUMERO, REPOSICION, NOMBRE, TURNOS) values (?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DbUtil.getConnection();
pstmt = conn.prepareStatement(sql);
Document doc = new SAXReader().read(new File("D:/share/JavaProjects/drp/test_xmlImport/xml/test01.XML"));
List itemList = doc.selectNodes("/ACCESOS/item/SOCIO");
for (Iterator iter=itemList.iterator(); iter.hasNext();) {
Element el = (Element)iter.next();
String numero = el.elementText("NUMERO");
String reposicion = el.elementText("REPOSICION");
String nombre = el.elementText("NOMBRE");
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);
}
pstmt.setString(1, numero);
pstmt.setString(2, reposicion);
pstmt.setString(3, nombre);
pstmt.setString(4, sbString.toString());
pstmt.addBatch();
}
pstmt.executeBatch();
System.out.println("将XML导入数据库成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtil.close(pstmt);
DbUtil.close(conn);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<ACCESOS>
<item>
<SOCIO>
<NUMERO>00045050</NUMERO>
<REPOSICION>0</REPOSICION>
<NOMBRE>MOISES MORENO</NOMBRE>
<TURNOS>
<LU>T1</LU>
<MA>T2</MA>
<MI>T3</MI>
<JU>T4</JU>
<VI>T5</VI>
<SA>T6</SA>
<DO>T7</DO>
</TURNOS>
</SOCIO>
</item>
<item>
<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>S7</DO>
</TURNOS>
</SOCIO>
</item>
</ACCESOS>
1、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import org.xml.sax.SAXException;
import java.io.*;
public class DomParserDemo{
private Document doc;
public DomParserDemo() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
String source= "e:/jhb1117/classes/xmldoc/candidate.xml ";
doc=builder.parse(source);
}
public void showDocument() {
//get all <person>
NodeList personList=doc.getElementsByTagName(XMLTagDir.NODE_PERSON); // "PERSON " 也可 ,本文中为数据词典
for(int i=0;i <personList.getLength();i++) //节点从0开始
{
Element person=(Element)personList.item(i);
System.out.print(XMLTagDir.NODE_NAME+ ": ");
System.out.println(getNodeValue(person,XMLTagDir.NODE_NAME));
System.out.print(XMLTagDir.NODE_ADDRESS+ ": ");
System.out.println(getNodeValue(person,XMLTagDir.NODE_ADDRESS));
System.out.print(XMLTagDir.NODE_TEL+ ": ");
System.out.println(getNodeValue(person,XMLTagDir.NODE_TEL));
System.out.print(XMLTagDir.NODE_FAX+ ": ");
System.out.println(getNodeValue(person,XMLTagDir.NODE_FAX));
System.out.print(XMLTagDir.NODE_EMAIL+ ": ");
System.out.println(getNodeValue(person,XMLTagDir.NODE_EMAIL));
System.out.println();
}
}
public String getNodeValue(Element person,String nodeName){
NodeList nameList=person.getElementsByTagName(nodeName);
Element name=(Element)nameList.item(0);
Text text=(Text)name.getFirstChild();
String value=text.getNodeValue();
return value;
}
public void saveDocument(String path) throws IOException
{
FileWriter fw=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fw);
fw.close();
}
public static void main(String args[]){
try{
DomParserDemo doc=new DomParserDemo();
doc.showDocument();
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/parseOut.xml ";
String path= "e:/jhb1117/classes/xmldoc/jhbparseOut.xml ";
doc.saveDocument(path);
System.out.print( "file saved ");
}catch(Exception e){
e.printStackTrace();
}
}
}
2、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import java.io.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class DomCreateDemo {
private Document doc;
public DomCreateDemo() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.newDocument();
}
public void createDocument(){
if(doc==null) return;
Element peopleElement=doc.createElement(XMLTagDir.NODE_PEOPLE);
for(int i=1;i <=3;i++){
Element personElement=doc.createElement(XMLTagDir.NODE_PERSON);
personElement.setAttribute( "PERSONID ", "E "+i);
//one person include several tags
Text text=null;
Element nameElement=doc.createElement(XMLTagDir.NODE_NAME);
text=doc.createTextNode( "myName "+i);
nameElement.appendChild(text);
personElement.appendChild(nameElement);
Element addressElement=doc.createElement(XMLTagDir.NODE_ADDRESS);
text=doc.createTextNode( "myAddress "+i);
addressElement.appendChild(text);
personElement.appendChild(addressElement);
Element telElement=doc.createElement(XMLTagDir.NODE_TEL);
text=doc.createTextNode( "myTel "+i);
telElement.appendChild(text);
personElement.appendChild(telElement);
Element faxElement=doc.createElement(XMLTagDir.NODE_FAX);
text=doc.createTextNode( "myFax "+i);
faxElement.appendChild(text);
personElement.appendChild(faxElement);
Element emailElement=doc.createElement(XMLTagDir.NODE_EMAIL);
text=doc.createTextNode( "myEmail "+i);
emailElement.appendChild(text);
personElement.appendChild(emailElement);
peopleElement.appendChild(personElement);
}
doc.appendChild(peopleElement);
}
public void saveDocument(String path) throws IOException {
FileWriter fout=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
}
public static void main(String[] args) {
try{
DomCreateDemo doc = new DomCreateDemo();
doc.createDocument();
System.out.print( "doc created ");
String path= "e:/jhb1117/classes/xmldoc/jhbcreateOut.xml ";
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/createOut.xml ";
doc.saveDocument(path);
System.out.print( "file saved ");
}catch(Exception e){
e.printStackTrace();
}
}
}
3、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import java.io.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class DomCreateDemo {
private Document doc;
public DomCreateDemo() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.newDocument();
}
public void createDocument(){
if(doc==null) return;
Element peopleElement=doc.createElement(XMLTagDir.NODE_PEOPLE);
for(int i=1;i <=3;i++){
Element personElement=doc.createElement(XMLTagDir.NODE_PERSON);
personElement.setAttribute( "PERSONID ", "E "+i);
peopleElement.appendChild(personElement);
}
doc.appendChild(peopleElement);
}
public void saveDocument(String path) throws IOException {
FileWriter fout=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
}
public static void main(String[] args) {
try{
DomCreateDemo doc = new DomCreateDemo();
doc.createDocument();
System.out.print( "doc created ");
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/createOut.xml ";
String path= "e:/jhb1117/classes/xmldoc/jhbcreateOut.xml ";
doc.saveDocument(path);
System.out.print( "file saved ");
}catch(Exception e){
e.printStackTrace();
}
}
}
4、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import org.xml.sax.SAXException;
import java.io.*;
public class DomParserDemo{
private Document doc;
public DomParserDemo() throws
IOException,ParserConfigurationException,SAXException{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
// Document doc=builder.parse( "resources/xmldoc/candidate.xml ");
// String source= "f:/houjie/JavaAdvance/dist/xmldoc/candidate.xml ";
String source= "e:/jhb1117/classes/xmldoc/candidate.xml ";
doc=builder.parse(source);
}
public void showDocument() {
//get all <person>
NodeList personList=doc.getElementsByTagName(XMLTagDir.NODE_PERSON);
for(int i=0;i <personList.getLength();i++){
Element person=(Element)personList.item(i);
System.out.print(XMLTagDir.NODE_NAME);
System.out.println(getNodeValue(person,XMLTagDir.NODE_NAME));
System.out.print(XMLTagDir.NODE_ADDRESS);
System.out.println(getNodeValue(person,XMLTagDir.NODE_ADDRESS));
System.out.print(XMLTagDir.NODE_TEL);
System.out.println(getNodeValue(person,XMLTagDir.NODE_TEL));
System.out.print(XMLTagDir.NODE_FAX);
System.out.println(getNodeValue(person,XMLTagDir.NODE_FAX));
System.out.print(XMLTagDir.NODE_EMAIL);
System.out.println(getNodeValue(person,XMLTagDir.NODE_EMAIL));
System.out.println();
}
}
public void showAndSavePeopleDocument(Document doc,String path) {
//get all <person>
NodeList personList=doc.getElementsByTagName(XMLTagDir.NODE_PERSON);
for(int i=0;i <personList.getLength();i++){
Element person=(Element)personList.item(i);
System.out.print(XMLTagDir.NODE_NAME);
System.out.println(getNodeValue(person,XMLTagDir.NODE_NAME));
System.out.print(XMLTagDir.NODE_ADDRESS);
System.out.println(getNodeValue(person,XMLTagDir.NODE_ADDRESS));
System.out.print(XMLTagDir.NODE_TEL);
System.out.println(getNodeValue(person,XMLTagDir.NODE_TEL));
System.out.print(XMLTagDir.NODE_FAX);
System.out.println(getNodeValue(person,XMLTagDir.NODE_FAX));
System.out.print(XMLTagDir.NODE_EMAIL);
System.out.println(getNodeValue(person,XMLTagDir.NODE_EMAIL));
System.out.println();
}
try{
saveDocument(doc,path);
}catch(Exception e){
e.printStackTrace();
}
}
public String getNodeValue(Element person,String nodeName){
NodeList nameList=person.getElementsByTagName(nodeName);
Element name=(Element)nameList.item(0);
Text text=(Text)name.getFirstChild();
String value=text.getNodeValue();
return value;
}
public void saveDocument(String path) throws IOException {
FileWriter fout=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
}
public void saveDocument(Document doc,String path) throws IOException {
FileWriter fout=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
}
public static void main(String args[]){
try{
DomParserDemo doc=new DomParserDemo();
doc.showDocument();
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/parseOut.xml ";
String path= "e:/jhb1117/classes/xmldoc/jhbparseOut.xml ";
doc.saveDocument(path);
System.out.print( "file saved ");
}catch(Exception e){
e.printStackTrace();
}
}
}
5、
package myxml;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
/*
NAME
ADDRESS
TEL
FAX
EMAIL
*/
public class DBPeople {
public DBPeople() {
}
public static final int NAME=1;
public static final int ADDRESS=2;
public static final int TEL=3;
public static final int FAX=4;
public static final int EMAIL=5;
private String name;
private String address;
private String tel;
private String fax;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTel() {
return tel;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getFax() {
return fax;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
6、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class DomTransform {
private Document doc;
private Transformer transformer;
public DomTransform() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
// String source= "e:/houjie/JavaAdvance/dist/xmldoc/candidate.xml ";
String source= "e:/jhb1117/classes/xmldoc/candidate.xml ";
doc=builder.parse(source);
TransformerFactory tf=TransformerFactory.newInstance();
transformer=tf.newTransformer();
}
public void changeDocument(){
//get all <person>
NodeList personList=doc.getElementsByTagName(XMLTagDir.NODE_PERSON);
for(int i=0;i <personList.getLength();i++){
Element person=(Element)personList.item(i);
setNodeValue(person,XMLTagDir.NODE_ADDRESS, "newAddress "+i); //改变address值
System.out.print(XMLTagDir.NODE_ADDRESS);
System.out.println(getNodeValue(person,XMLTagDir.NODE_ADDRESS));
}
}
public void setNodeValue(Element person,String nodeName,String newValue){
NodeList nameList=person.getElementsByTagName(nodeName);
Element name=(Element)nameList.item(0);
Text text=(Text)name.getFirstChild();
text.setNodeValue(newValue);
}
public String getNodeValue(Element person,String nodeName){
NodeList nameList=person.getElementsByTagName(nodeName);
Element name=(Element)nameList.item(0);
Text text=(Text)name.getFirstChild();
String value=text.getNodeValue();
return value;
}
public void saveDocument(String path) throws IOException,TransformerException {
FileWriter fout=new FileWriter(path);
DOMSource source=new DOMSource(doc);
StreamResult result=new StreamResult(fout);
transformer.transform(source,result);
/* XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
*/ fout.close();
}
public static void main(String[] args) {
try{
DomTransform doc = new DomTransform();
doc.changeDocument();
System.out.println( "doc changed ");
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/transformOut.xml ";
String path= "e:/jhb1117/classes/xmldoc/transformOut.xml ";
doc.saveDocument(path);
System.out.print( "file saved ");
}catch(Exception e){
e.printStackTrace();
}
7、
package myxml;
import java.io.Serializable;
import java.sql.*;
import oracle.jdbc.driver.OracleDriver;
import java.util.*;
public class PeopleProcessor implements Serializable{
private Connection conn;
private Statement stmt;
private String url= "jdbc:oracle:oci8:@orcl817 ";
private String user= "scott ";
private String pwd= "tiger ";
public PeopleProcessor() throws SQLException{
initdb();
System.out.println( "connected ");
}
private void initdb()throws SQLException{
DriverManager.setLoginTimeout(10);
try{
// Driver driver=new oracle.jdbc.driver.OracleDriver();
// DriverManager.registerDriver(driver);
Class.forName( "oracle.jdbc.driver.OracleDriver ");
conn=DriverManager.getConnection(url,user,pwd);
conn.setAutoCommit(false);
stmt=conn.createStatement();
}catch(ClassNotFoundException e){
e.printStackTrace();
throw new SQLException( "driver class not found ");
}catch(SQLException e){
e.printStackTrace();
throw new SQLException( "db connection failed ");
}
}
public ResultSet doQuery(String sql)throws SQLException{
ResultSet rs=null;
rs=stmt.executeQuery(sql);
return rs;
}
public int doUpdate(String sql)throws SQLException{
int n;
n=stmt.executeUpdate(sql);
conn.commit();
System.out.println(n+ " records updated\n ");
return n;
}
public int insertPeople(DBPeople people)throws SQLException
{
String sql= "insert into people values(?,?,?,?,?) ";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(DBPeople.NAME,people.getName());
ps.setString(DBPeople.ADDRESS,people.getAddress());
ps.setString(DBPeople.TEL,people.getTel());
ps.setString(DBPeople.FAX,people.getFax());
ps.setString(DBPeople.EMAIL,people.getEmail());
int n=ps.executeUpdate();
ps.close();
return n;
}
public void close() throws SQLException{
if(conn!=null) conn.close();
if(stmt!=null) stmt.close();
}
public void commit()throws SQLException{
if(conn!=null)
conn.commit();
}
public static String toChinese(String strValue){
try{
if(strValue==null){
return null;
}
else{
byte[] bytes=strValue.getBytes( "ISO8859 ");
return new String(bytes, "GKB ");
}
}catch(Exception e){
return null;
}
}
public List retrievePeople()throws SQLException{
List peopleList=new LinkedList();
String sql= "select * from people ";
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int columns=meta.getColumnCount();
int n=0;
while(rs.next()){
DBPeople people=new DBPeople();
people.setName(rs.getString(DBPeople.NAME));
people.setAddress(rs.getString(DBPeople.ADDRESS));
people.setTel(rs.getString(DBPeople.TEL));
people.setFax(rs.getString(DBPeople.FAX));
people.setEmail(rs.getString(DBPeople.EMAIL));
peopleList.add(people);
}
return peopleList;
}
public static void main(String args[]){
try{
PeopleProcessor jp=new PeopleProcessor();
// ResultSet rs=jp.doQuery( "select * from emp where empno= '0001 ' ");
ResultSet rs=jp.doQuery( "select * from people ");
ResultSetMetaData meta=rs.getMetaData();
int columns=meta.getColumnCount();
int n=0;
while(rs.next()){
for(int i=1;i <=columns;i++){
System.out.print(rs.getString(i)+ "\t ");
}
n++;
System.out.println();
}
System.out.println(n+ " rows selcted ");
List peopleList=jp.retrievePeople();
System.out.println( "there are " +peopleList.size()+ " records in people ");
jp.close();
}catch(SQLException e){}
}
}
8、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import java.io.*;
import java.util.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class XMLFromJdbc implements Serializable{
private PeopleProcessor dbProcessor;
private Document doc;
private List peopleList;
private boolean isDocCreated=false;
public XMLFromJdbc() throws Exception{
dbProcessor=new PeopleProcessor();
peopleList=dbProcessor.retrievePeople(); //执行select * from people
dbProcessor.close();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.newDocument();
}
public void createDocument(){
if(doc==null) return;
if(isDocCreated) return;
Element peopleElement=doc.createElement(XMLTagDir.NODE_PEOPLE);
if(peopleList==null) return;
Iterator iter=peopleList.iterator(); //游标Iterator
int no=1;
while(iter.hasNext()){ //Tierator的方法hasNext()
DBPeople people=(DBPeople)iter.next(); //Tierator的方法Next()
Element personElement=doc.createElement(XMLTagDir.NODE_PERSON);
personElement.setAttribute( "PERSONID ", "E "+no);
//one person include several tags
Text text=null;
Element name=doc.createElement(XMLTagDir.NODE_NAME);
text=doc.createTextNode(people.getName());
name.appendChild(text);
personElement.appendChild(name);
Element address=doc.createElement(XMLTagDir.NODE_ADDRESS);
text=doc.createTextNode(people.getAddress());
address.appendChild(text);
personElement.appendChild(address);
Element tel=doc.createElement(XMLTagDir.NODE_TEL);
text=doc.createTextNode(people.getTel());
tel.appendChild(text);
personElement.appendChild(tel);
Element fax=doc.createElement(XMLTagDir.NODE_FAX);
text=doc.createTextNode(people.getFax());
fax.appendChild(text);
personElement.appendChild(fax);
Element email=doc.createElement(XMLTagDir.NODE_EMAIL);
text=doc.createTextNode(people.getEmail());
email.appendChild(text);
personElement.appendChild(email);
peopleElement.appendChild(personElement);
no++;
}
doc.appendChild(peopleElement);
isDocCreated=true;
}
public void saveDocument(String path) throws IOException {
FileWriter fout=new FileWriter(path);
XmlDocument xmldoc=(XmlDocument)doc;
xmldoc.write(fout);
fout.close();
}
public Document getDocument(){
if(!isDocCreated)
this.createDocument();
return this.doc;
}
public static void main(String[] args) {
try{
XMLFromJdbc doc = new XMLFromJdbc();
doc.createDocument();
System.out.println( "doc created ");
// String path= "e:/houjie/JavaAdvance/dist/xmldoc/XMLFromJdbc.xml ";
String path= "e:/jhb1117/classes/xmldoc/XMLFromJdbc.xml ";
doc.saveDocument(path);
System.out.println( "file saved ");
System.out.println(doc.peopleList.size()); //
}catch(Exception e){
e.printStackTrace();
}
}
}
9、
| ||
10、
package myxml;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class XMLTagDir {
public final static String NODE_PEOPLE= "PEOPLE ";
public final static String NODE_PERSON= "PERSON ";
public final static String NODE_NAME= "NAME ";
public final static String NODE_ADDRESS= "ADDRESS ";
public final static String NODE_TEL= "TEL ";
public final static String NODE_FAX= "FAX ";
public final static String NODE_EMAIL= "EMAIL ";
private XMLTagDir() {
}
}
11、
package myxml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import java.io.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2002 </p>
* <p> Company: </p>
* @author xxy
* @version 1.0
*/
public class XMLToJdbc {
private PeopleProcessor dbProcessor;
private Document doc;
public XMLToJdbc() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
// String source= "e:/houjie/JavaAdvance/dist/xmldoc/candidate.xml ";
String source= "e:/jhb1117/classes/xmldoc/candidate.xml ";
doc=builder.parse(source);
dbProcessor=new PeopleProcessor();
}
public void createRecords() throws Exception{
NodeList personList=doc.getElementsByTagName( "XMLTagDir.NODE_PERSON ");
DBPeople[] people=new DBPeople[personList.getLength()];
for(int i=0;i <personList.getLength();i++){
people[i]=new DBPeople();
Element person=(Element)personList.item(i);
Text text;
//one <person> include several tags
NodeList nameList=person.getElementsByTagName( "XMLTagDir.NODE_NAME ");
Element name=(Element)nameList.item(0);
text=(Text)name.getFirstChild();
people[i].setName(text.getNodeValue());
NodeList addressList=person.getElementsByTagName( "XMLTagDir.NODE_ADDRESS ");
Element address=(Element)addressList.item(0);
text=(Text)address.getFirstChild();
people[i].setAddress(text.getNodeValue());
NodeList telList=person.getElementsByTagName( "XMLTagDir.NODE_TEL ");
Element tel=(Element)telList.item(0);
text=(Text)tel.getFirstChild();
people[i].setTel(text.getNodeValue());
NodeList faxList=person.getElementsByTagName( "XMLTagDir.NODE_FAX ");
Element fax=(Element)faxList.item(0);
text=(Text)fax.getFirstChild();
people[i].setFax(text.getNodeValue());
NodeList emailList=person.getElementsByTagName( "XMLTagDir.NODE_EMAIL ");
Element email=(Element)emailList.item(0);
text=(Text)email.getFirstChild();
people[i].setEmail(text.getNodeValue());
}
//insert people
for (int i=0 ;i <people.length;i++)
{
dbProcessor.insertPeople(people[i]);
System.out.println( "inserted one record ");
}
dbProcessor.commit();
dbProcessor.close();
}
public static void main(String[] args) {
try{
XMLToJdbc doc = new XMLToJdbc();
doc.createRecords();
System.out.println( "insert finished ");
}catch(Exception e){
e.printStackTrace();
}
}
}