Ⅰ *.xml文件如何導入access資料庫,並在窗口中顯示
Access寫代碼導出襲/導入XML文件
'導出時:
Private Sub 導出_Click()
Application.ExportXML acExportTable, "表名", "D:\Data.XML", "DataSchema.xml"
End Sub
'導入時:
Private Sub 導入_Click()
Application.ImportXML "D:\Data.xml", acAppendData
'把數據添加到原有的表
Application.ImportXML "D:\Data.xml", acStructureAndData
'默認,建立Data表並導入數據
Application.ImportXML "D:\Data.xml", acStructureOnly
'只建立Data表結構,無數據
End Sub
Ⅱ 從oracle資料庫查詢數據生成xml,java解析xml文件的數據批量插入到oracle資料庫(用SAX解析)
我自己前段時間寫的有事就問
資料庫建了一個表study
id student class teacher age 這5個欄位
package com.sql.xml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
//導出數據存入XML
public class ReadDb {
public static void main(String args[]){
//連接資料庫
try{
//資料庫連接
String url="jdbc:oracle:thin:@10.0.3.18:1521:hxtest18";
String classforname="oracle.jdbc.driver.OracleDriver";
String usename="yong";
String password="password";
Class.forName(classforname);
Connection con=DriverManager.getConnection(url,usename,password);
Statement sta=con.createStatement();
//查詢數據
ResultSet result=sta.executeQuery(
"SELECT * FROM study");
//創建根節點
Document document=DocumentHelper.createDocument();
Element studyInfo=DocumentHelper.createElement("studyInfo");
document.setRootElement(studyInfo);
//Element studyInfo=document.addElement("studyInfo");
//當有值時查詢
while(result.next()){
//增加每個節點
Element study=studyInfo.addElement("study");
Element id=study.addElement("id");
Element student=study.addElement("student");
Element classe=study.addElement("class");
Element teacher=study.addElement("teacher");
Element age=study.addElement("age");
//將資料庫值通過SETTEXT為XML值
id.setText(result.getString("id"));
student.setText(result.getString("student"));
//System.out.println(id.getText());
classe.setText(result.getString("class"));
teacher.setText(result.getString("teacher"));
age.setText(result.getString("age"));
}
//將樹導出到文件中:兩種輸出方法
XMLWriter write=new XMLWriter(new FileOutputStream(new File("studyInfo.xml")));
write.write(document);
write.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
插入資料庫,改了幾個條件
bookstore.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book has="100">
<bname>資料庫</bname>
<price>33.80元</price>
<date>2009-10-1</date>
</book>
<book has="95">
<bname>三國演義</bname>
<price>40.00元</price>
<date>2005-1-7</date>
</book>
<book has="86">
<bname>西遊記</bname>
<price>30.00元</price>
<date>2004-3-8</date>
</book>
<cd have="15">
<id>1</id>
<cname>beyound</cname>
<owner>小黎</owner>
</cd>
<cd have="17">
<id>2</id>
<cname>kugou</cname>
<owner>小強</owner>
</cd>
</bookstore>
解析如下
package com.sql.xml;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
//XML的解析與轉換後存入資料庫
public class Fenkai {
public static void main(String args[]){
String sql,BNAME=null,PRICE=null,DAT=null;//定義資料庫表段
String sqlatt,CNAME=null,ID=null,OWNER=null;
//讀取xml文件
SAXReader reader=new SAXReader();
File file=new File("bookstore.xml");
try{
//資料庫連接
String url="jdbc:oracle:thin:@10.0.3.18:1521:hxtest18";
String classforname="oracle.jdbc.driver.OracleDriver";
String usename="yong";
String password="password";
Class.forName(classforname);
Connection con=DriverManager.getConnection(url,usename,password);
//解析xml文件
Document document=reader.read(file);
Element root=document.getRootElement();//根節點
System.out.println("輸出根節點:"+root.getName());
//下面用迭代得到子節點和孫節點
for(Iterator child=root.elementIterator();child.hasNext();){
Element childEle=(Element)child.next();
System.out.println("子:"+childEle.getName());
System.out.println("孫:");
for(Iterator grand=childEle.elementIterator();grand.hasNext();){
Element grandEle=(Element)grand.next();
System.out.println(grandEle.getName()+"值:"+grandEle.getText());
//轉換數據
while(grandEle.getText().equals("資料庫")){
grandEle.setText("001");
}
while(grandEle.getText().equals("三國演義")){
grandEle.setText("002");
}
while(grandEle.getText().equals("西遊記")){
grandEle.setText("003");
}
while(grandEle.getText().equals("beyound")){
grandEle.setText("004");
}
while(grandEle.getText().equals("kugou")){
grandEle.setText("005");
}
}}
//得到各節點值插入資料庫
for(Iterator it= root.elementIterator("book");it.hasNext();){
Element ch=(Element)it.next();
BNAME=ch.elementText("bname");
PRICE=ch.elementText("price");
DAT=ch.elementText("date");
// while(childEle.getName().equals("cd")){}
sql="insert into book values('"+BNAME+"','" +PRICE
+"','"+DAT+"')";
//sql="select * from book";//加入本語句運行不改變數據表
//執行
con.createStatement().executeUpdate(sql);}
for(Iterator itt= root.elementIterator("cd");itt.hasNext();){
Element cht=(Element)itt.next();
CNAME=cht.elementText("cname");
ID=cht.elementText("id");
OWNER=cht.elementText("owner");
sqlatt="insert into cd values('"+CNAME+"','" +ID
+"','"+OWNER+"')";
//sqlatt="select * from cd";
con.createStatement().executeUpdate(sqlatt);}
//將轉換後文件存入txt
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("gbk");
XMLWriter writer = new XMLWriter( new FileWriter(new File("bookchange.txt")),format);
writer.write(document);
writer.close();
System.out.println(document.asXML());//列印改變後的樹
}catch(Exception e){
e.printStackTrace();
System.out.println("有錯啊!");
}
}
}
Ⅲ Java中用sql語句將xml文件導入 Access 資料庫,急用!謝謝
sql語句好像沒這導入的功能吧,插入那是人工的方法,的自己寫,不過資料庫好像都版可以使用客戶端進行權導入的;想mysql就有Natcat for mysql 可以直接導入xml文件到資料庫的,好像Access要寫程序導入。
Ⅳ 如何用java解析xml文檔,然後將數據存到資料庫里
package test11;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class XMLUtil
{
//該方法用於從XML配置文件中提取具體類類名,並返回一個實例對象
public static Object getBean()
{
try
{
//創建文檔對象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("config.xml"));
//獲取包含類名的文本節點
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//通過類名生成實例對象並將其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
<?xml version="1.0"?>
<config>
<className>test11.CatAdapter</className>
</config>
然後你吧解析處理的值放到數組或LIST或其他的你能存放的對象中。再寫SQL插入到資料庫就好了啊。主要資料庫事務處理或用批處理
Ⅳ java 伺服器端掃描文件夾下xml文件,並導入到Oracle資料庫
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class CreateXMLForEnterServelet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//獲取傳遞過來的值
String databaseType = request.getParameter("select");
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
String dbName = request.getParameter("dbName");
String dbSouse = request.getParameter("dbSouces");
int port = Integer.parseInt(request.getParameter("portNum"));
Document doc;
Element root;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String path = request.getRealPath("/");
File file = new File( path + "/" + "DBInfo.xml");
if(!file.exists())
{
doc = db.newDocument();
root = doc.createElement("DBInfo");
doc.appendChild(root);
}
else
{
doc = db.parse(file);
root = doc.getDocumentElement();
}
//添加子節點
Element rowNode = doc.createElement(databaseType);
root.appendChild(rowNode);
Node node = doc.createElement("Driver");
Node nodestr = doc.createElement("ConnectString");
Node serverName = doc.createElement("ServerName");
Node nodeName = doc.createElement("UserName");
Node nodePWD = doc.createElement("PassWord");
Node nodePort = doc.createElement("Port");
Node nodeDB = doc.createElement("DataBase");
rowNode.appendChild(node);
rowNode.appendChild(nodestr);
rowNode.appendChild(serverName);
rowNode.appendChild(nodeDB);
rowNode.appendChild(nodeName);
rowNode.appendChild(nodePWD);
rowNode.appendChild(nodePort);
String nodeValue;//Driver
String nodeConStr;//連接字元串
if(databaseType.equals("ORACLE"))
{
nodeValue = "oracle.jdbc.driver.OracleDriver";
nodeConStr = "jdbc:oracle:thin";
}
else
{
nodeValue = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
nodeConStr = "jdbc:microsoft:sqlserver";
}
Text text = doc.createTextNode(nodeValue);
Text textstr = doc.createTextNode(nodeConStr);
Text textName = doc.createTextNode(userName);
Text textPWD = doc.createTextNode(userPassword);
Text textServer = doc.createTextNode(dbName);
Text textPort = doc.createTextNode(String.valueOf(port));
Text textDB = doc.createTextNode(dbSouse);
node.appendChild(text);
nodestr.appendChild(textstr);
nodeName.appendChild(textName);
nodePWD.appendChild(textPWD);
serverName.appendChild(textServer);
nodePort.appendChild(textPort);
nodeDB.appendChild(textDB);
DOMSource doms = new DOMSource(doc);
StreamResult sr = null;
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
sr = new StreamResult(file);
tf.transform(doms,sr);
out.println("<h1>Create XMLFile Is Ok!!</h1>");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
catch(Exception ex)
{
out.println("<h1>生成 XML 配置文件時出現錯誤:</h1><br/><h4>"+ex+"</h4>");
out.println("<br/><br/>");
out.print("<a href='index.jsp'>【點這里回到首頁】</a>");
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}