/* * Created on 2006-8-13 * TODO-Change the template for this generated file go to * Window -Preferences-java-code style-code Templates */
Package Com.hotmail.scbit.SAX;
Import java.util.*;
Import org.xml.sax.*;
Import Org.xml.sax.helpers.DefaultHandler;
Resolution class public class Dbcontactshandler extends DefaultHandler {Contacts contacts = null;
Private stack tagsstack=new stack ();
Private vector contacts = new vector ();
Private String ID;
private String name;
Private String project;
Private String telephone;
Private String Email;
Private String MSN;
Private String remark;
Public Vector getcontacts () {return this.contacts; public void Startelement (String uri,string localname, String qname,attributes a
Ttrs) throws Saxexception {Tagsstack.push (qName);
} public void characters (char[] ch,int start,int length) throws Saxexception {
String tag= (String) Tagsstack.peek ();
if (Tag.equals ("id")) {this.id=new String (ch,start,length);
} if (Tag.equals ("name")) {this.name=new String (ch,start,length);
} if (Tag.equals ("project")) {this.project = new String (ch,start,length);
} if (Tag.equals ("Telephone")) {this.telephone=new String (ch,start,length); } if (tag.equals ("email")) {this.email=new String (
Ch,start,length);
} if (Tag.equals ("MSN")) {this.msn=new String (ch,start,length);
} if (Tag.equals ("remark")) {this.remark=new String (ch,start,length);
} public void EndElement (String uri,string localname,string qName) throws Saxexception {
String tag= (String) tagsstack.pop (); if (tag.equalSignorecase ("Employe")) {contact = new Contacts (Integer.parseint (thi s.id), this.name, This.project, This.telephone, This.email
, This.msn, This.remark);
Contacts.add (contact);
System.out.println (this.id);
System.out.println (this.name);
System.out.println (This.project);
System.out.println (This.telephone);
System.out.println (This.email);
System.out.println (THIS.MSN);
System.out.println (This.remark);
} public void Enddocument () throws Saxexception {System.out.println ("found out");
}//Document Ditto//documents in parser to parse package Com.hotmail.scbit.SAX;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import Java.io.File; Import Java.util.VecTor
public class Dbcontactsparser {private Final file = new file ("E://java object//contacts.xml");
Vector contacts = new vector ();
private void Parse () {try {Dbcontactshandler handler = new Dbcontactshandler ();
SAXParserFactory spfactory = Saxparserfactory.newinstance ();
SAXParser sp = Spfactory.newsaxparser ();
Sp.parse (File,handler);
contacts = Handler.getcontacts ();
catch (Exception e) {e.printstacktrace ();
()} public Vector Getcontactinfo () {this.parse ();
return this.contacts;
}
}
Package Com.hotmail.scbit.DOM; Import java.sql.*; Import Java.io.File; Import Javax.xml.parsers.DocumentBuilderFactory; Import Javax.xml.parsers.DocumentBuilder; Import Javax.xml.transform.TransformerFactory; Import Javax.xml.transform.Transformer; Import Javax.xml.transform.dom.DOMSource; Import Javax.xml.transform.stream.StreamResult; Import org.w3c.dom.*; public class Xmldocumenrtbuilder {//Generate XML document based on result set public static void Tableinput (ResultSet rs,file XMLFile) {try {ResultS Etmetadata RSDA = Rs.getmetadata (); int count = Rsda.getcolumncount (); String columnname[] = new String[count]; int columntype[] = new Int[count]; for (int i = 0;i < count i + +) {Columnname[i] = Rsda.getcolumnname (i+1); Columntype[i] = Rsda.getcolumntype (i+1); System.out.println (Columnname[i]); System.out.println (Columntype[i]); } documentbuilderfactory doc = Documentbuilderfactory.newinstance (); Documentbuilder Docbuilder = Doc.newdocumentbuilder (); Create a new text object Document xmldoc = Docbuilder.newdocumENT (); Element clomnelement = null; Field name Element rowelement = null;//child element Name text = null;//field value//establish root elements element Employesnode = Xmldoc.createelement ("Employe Es "); while (Rs.next ()) {//establishes the child element rowelement = xmldoc.createelement ("Employe"); for (int i = 0;i < count; i + +) {Clomnelement = Xmldoc.createelement (Columnname[i]); if (columntype[i] = = 4) {text = Xmldoc.createtextnode (string.valueof (Rs.getint (i + 1)))} if (columntype[i) = =) {text = Xmldoc.createtextnode (string.valueof (rs.getstring (i + 1))); } clomnelement.appendchild (text); Rowelement.appendchild (clomnelement); } employesnode.appendchild (Rowelement); } xmldoc.appendchild (Employesnode); Transformerfactory tarnsfactory = Transformerfactory.newinstance (); Transformer Tarnsformer = Tarnsfactory.newtransformer (); Domsource Xmldomsource = new Domsource (xmldoc); Streamresult bookstarment = new Streamresult (XMLFile); Tarnsformer.transform (xmldomsource,bookstarment); System.out.println ("Tarnsformer createelement"); catch (ExceptiOn e) {e.printstacktrace ();}} Parse the XML document and save it to the database public static void Tableoutput (Connection con,file XMLFile) {PreparedStatement PST = NULL; String strSQL = "INSERT into contacts values (?,?,?,?,?,?,?)"; int id = 0; String name = NULL; String project = null; String telephone = null; String email = null; String MSN = null; String remark = null; try {PST = con.preparestatement (strSQL); Documentbuilderfactory documentfactory = Documentbuilderfactory.newinstance (); Documentbuilder Documentbuilder = Documentfactory.newdocumentbuilder (); Document doc = Documentbuilder.parse (XMLFile); Element employeelement = Doc.getdocumentelement (); NodeList Employenode = Employeelement.getchildnodes (); for (int i = 0; i < employenode.getlength (); i = i + 1) {nodelist stunodelist = Employenode.item (i). Getchildnodes (); fo R (Int j = 0;j < Stunodelist.getlength (); j = j + 1) {element col = (Element) Stunodelist.item (j); if (j = = 0) {id = inte Ger.parseint (Col.getchildnodes (). Item (0). Getnodevalue ()); } if (j = = 1) {name = Col.getchildnodes (). Item (0). Getnodevalue ();} if (j = = 2) {Project =col.getchildnodes (). Item (0). Getnodeva Lue (); } if (j = = 3) {telephone = Col.getchildnodes (). Item (0). Getnodevalue ();} if (j = = 4) {email = col.getchildnodes (). Item (0). g Etnodevalue (); } if (j = = 5) {MSN = Col.getchildnodes (). Item (0). Getnodevalue ();} if (j = = 6) {remark = Col.getchildnodes (). Item (0). GetNode Value (); } pst.setint (1,id); Pst.setstring (2,name); Pst.setstring (3,project); Pst.setstring (4,telephone); Pst.setstring (5, email); Pst.setstring (6, MSN); Pst.setstring (7, remark); Pst.addbatch (); } pst.executebatch (); SYSTEM.OUT.PRINTLN ("Database execution completed"); catch (Exception e) {e.printstacktrace (); System.out.println ("Bunengcharu:");}} XML document <?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> <employees> <employe> <id>1 </id> <name>babnsdf</name> <project> Temporary no </project> <telephone>153********</ Telephone> <email>gao@vip.sina.com</eMail> <MSN>jun@hotmail.com</MSN> <reach> No Comments </reach> </employe> <employe> <id>2</id> <name>student</name> <project> specimen gallery </project> <telephone>137** </telephone> <email>y@scbit.org</email> <MSN>wwx@hotmail.com</MSN> <reach > No Comments </reach> </employe> <employe> <id>3</id> <name>zhanzhan</name> <project>his, Lis and other comprehensive inquiries </project> <telephone>131********</telephone> <email> Tan@scbit.org</email> <MSN>mose@msn.com</MSN> <reach> No Comments </reach> </employe> <employe> <id>4</id> <name> class red </name> <project> pathogen identification </project> < Telephone>133********</telephone> <email>ban@yahoo.com.cn</email> <msn>ban@msn.com </MSN> <reach> No Comments </reach> </employe> <employe> <id>5</id> <name> WuGUI Qiang </name> <project> no </project> <telephone>136********</telephone> <email> Vriu@hotmail.com</email> <MSN>Vriu@hotmail.com</MSN> <reach> No Comments </reach> </ employe> <employe> <id>6</id> <name>zhaowu</name> <project> Molecular typing </project > <telephone>137********</telephone> <email>jia@hotmail.com</email> <MSN> Jia@hotmail.com</msn> <reach> No notes </reach> </employe> <employe> <id>7</id > <name>lisi</name> <project> Temporary no </project> <telephone>158********</telephone > <email>liu@163.com</email> <MSN>chn@hotmail.com</MSN> <reach> No Comments </reach > </employe> <employe> <id>8</id> <name>zhangsan</name> <project> No </project> <telephone>137********</telephone> <email>xian@gmail.com</email> <msn >aNge@hotmail.com</msn> <reach> No notes </reach> </employe> <employe> <id>9</id > <name>baijuyi</name> <project> Temporary no </project> <telephone>137********</ telephone> <email>a1a@163.com</email> <MSN>a1-@163.com</MSN> <reach> No Comments </ reach> </employe> <employe> <id>10</id> <name>wanganshi</name> <project > No </project> <telephone>137********</telephone> <email>dug@tom.com</email> < Msn>mes@hotmial.com</msn> <reach> No notes </reach> </employe> <employe> <id>11 </id> <name>ouyangxiu</name> <project> No </project> <telephone>138********< /telephone> <email>chen@hotmail.com</email> <MSN>chen@hotmail.com</MSN> <reach> No notes </reach> </employe> <employe> <id>12</id> <name>dupu</name> <project> </project> <telephone>135********</telephone> <email>xin@gmail.com</email > <MSN>yanyan@yahoo.com.cn</MSN> <reach> No comment </reach> </employe> <employe> <id>13</id> <name>libai</name> <project> Temporary no </project> <telephone>159**** </telephone> <email>papa@hotmail.com</email> <MSN>papa@hotmail.com</MSN> < Reach> No notes </reach> </employe> </employees>