import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import java.sql.*;
public class Example1{
int id[]=null;
String name[]=null;
String addr[]=null;
String tel[]=null;
public static void main(String[] args) {
try{
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
File f=new File("Example1.xml");
Document document=documentBuilder.parse(f);
Example1 ex=new Example1();
ex.doDOM(document); //提取XML文档里的数据
ex.writeSQL(); //将提取的数据写入MySql
}catch(Exception e){
System.out.print(e.getMessage());
}
}
private void doDOM(Document document)
{
Element root=document.getDocumentElement();
NodeList nodeList=root.getElementsByTagName("id");
int size=nodeList.getLength();
id=new int[size];
name=new String[size];
addr=new String[size];
tel=new String[size];
for(int i=0;i<size;i++)
{
Node node=nodeList.item(i);
String content=node.getTextContent();
id[i]=new Integer(content).intValue();
}
nodeList=root.getElementsByTagName("name");
for(int i=0;i<size;i++)
{
Node node=nodeList.item(i);
String content=node.getTextContent();
name[i]=content;
}
nodeList=root.getElementsByTagName("addr");
for(int i=0;i<size;i++)
{
Node node=nodeList.item(i);
String content=node.getTextContent();
addr[i]=content;
}
nodeList=root.getElementsByTagName("tel");
for(int i=0;i<size;i++)
{
Node node=nodeList.item(i);
String content=node.getTextContent();
tel[i]=content;
}
}
private void writeSQL()
{
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/student?user=root&password=tmq&characterEncoding=gb2312";
Connection conn=DriverManager.getConnection(url);
Statement st=conn.createStatement();
String sql=null;
for(int k=0;k<id.length;k++)
{
sql="insert into students values('"+id[k]+"','"+name[k]+"','"+addr[k]+"','"+tel[k]+"')";
st.executeUpdate(sql);
}
st.close();
conn.close();
}catch(Exception eee)
{
System.out.println(eee.toString());
}
}
}
一些xml开发示例,包括DTD,CSs,xslt,dom sax 等。都可以运行
4星 · 超过85%的资源 需积分: 9 59 浏览量
2009-08-28
08:41:07
上传
评论
收藏 599KB RAR 举报
THINKBIRD321
- 粉丝: 0
- 资源: 1
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈