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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
* <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;
}}// javac -d ..\classes .\myxml\DBPeople.java
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();
}
}
}
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){}
}
}
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();
}
}
}
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import myxml.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author xxy
* @version 1.0
*/public class XMLFromJdbc2 implements Serializable{
private List peopleList; public XMLFromJdbc2() throws Exception{
} public Document getDocument(){
Document doc;
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.newDocument(); PeopleProcessor dbProcessor =new PeopleProcessor();
peopleList=dbProcessor.retrievePeople();
dbProcessor.close();
}catch(Exception e){
e.printStackTrace();
return null;
}
Element peopleElement=doc.createElement(XMLTagDir.NODE_PEOPLE); if(peopleList==null) return null;
Iterator iter=peopleList.iterator();
int no=1;
while(iter.hasNext()){
DBPeople people=(DBPeople)iter.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);
return doc;
}
}
* <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() {
}
}
//javac -d ..\classes .\myxml\XMLTagDir.java
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();
}
}
}
谢谢你贴出来的代码,我正需要这样的东东呢,很及时,真的谢谢
昨天我比较忙,没有上来看,不知道大家已经“打”成一锅糨糊了
可能你觉得有时候被别人给冤枉了,所以你的反应很激烈,这时候刚刚有个针尖碰麦芒的机会,所以就叫劲起来了。
其实没有必要,大家都是本着技术共享技术交流的目的跑到这里来的,而且我觉得大环境都还不错,没必要因一时之气而互相敌视的,熄熄火吧,难道互相出口伤人的感觉就那么好希望我们大家共同进步,不单单是技术方面的
[email protected]
话到这里为止,怎么做人是你自己的问题。我也不会在跟贴下去的。
我又给你发了一遍