當前位置:首頁 » 編程語言 » sqlserver導入xml

sqlserver導入xml

發布時間: 2023-06-03 22:37:20

㈠ 從ftp上下載每天的xml文件到本地,再解析本地的xml文件中的數據並將其存入sqlserver資料庫

我給你個解析的代碼吧~具體存資料庫得根據實際情況來
#include "StdAfx.h"
#include "parse.h"
#include <string>
#include "stdafx.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <iostream>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>

using namespace std;

parse::parse()
//int parse::parsexml()
{

xmlDocPtr doc; //定義解析文檔指針

xmlNodePtr curNode; //定義結點指針(你需要它為了在各個結點間移動)

xmlChar *szKey; //臨時字元串變數

char *szDocName;

doc = xmlReadFile("SimACQ_Config.xml","GB2312",XML_PARSE_RECOVER); //解析文件

//檢查解析文檔是否成功,如果不成功,libxml將指一個注冊的錯誤並停止。

if (NULL==doc)

{

fprintf(stderr,"Document not parsed successfully. /n");

/*return -1;*/

}

curNode = xmlDocGetRootElement(doc); //確定文檔根元素

/*檢查確認當前文檔中包含內容*/

if (NULL == curNode)

{

fprintf(stderr,"empty document/n");

xmlFreeDoc(doc);

/*return -1;*/

}

/*在這個例子中,我們需要確認文檔是正確的類型。「root」是在這個示例中使用文檔的根類型。*/

if (xmlStrcmp(curNode->name, BAD_CAST "SIMCONFIG"))

{

fprintf(stderr,"document of the wrong type, root node != mail");

xmlFreeDoc(doc);

/*return -1; */

}

curNode = curNode->xmlChildrenNode;

xmlNodePtr propNodePtr = curNode;

while(curNode != NULL)

{

//取出節點中的內容

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"ComConfig")))
{

xmlNodePtr comConfigPtr= curNode->children;

while(comConfigPtr!=NULL)

{

if((!xmlStrcmp(comConfigPtr->name,(const xmlChar *)"DMS")))

{
xmlChar* szAttr = xmlGetProp(comConfigPtr,BAD_CAST "IP");

IP=(char*)szAttr;

szAttr=xmlGetProp(comConfigPtr,BAD_CAST "PORT");

PORT=atoi((const char *)szAttr);

szAttr=xmlGetProp(comConfigPtr,BAD_CAST "TIMEOUT");

TIMEOUT=atoi((const char *)szAttr);

xmlFree(szAttr);
}

comConfigPtr=comConfigPtr->next;
}
}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"Log")))
{
xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Flag");

if(szAttr!=NULL)
{

if((!xmlStrcmp(szAttr,(const xmlChar *)"True")))

{
FLAG=true;
}
else
{
FLAG=false;
}
}

szAttr = xmlGetProp(curNode,BAD_CAST "Path");

if(szAttr!=NULL)
{
PATH=(char*)szAttr;
}

xmlFree(szAttr);

}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"DMS")))
{
xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Cache");

if(szAttr!=NULL)
{

Cache=atoi((const char *)szAttr);
}
xmlFree(szAttr);
}

if ((!xmlStrcmp(curNode->name, (const xmlChar *)"SimFile")))
{
xmlChar* szAttr = xmlGetProp(curNode,BAD_CAST "Type");

if(szAttr!=NULL)
{
if((!xmlStrcmp(szAttr,(const xmlChar *)"PlainFilm")))
{

xmlNodePtr FileNodes=curNode->children;

int i=0;

while(FileNodes!=NULL)
{
if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))
{
szAttr = xmlGetProp(FileNodes,BAD_CAST "name");

if(szAttr!=NULL)
{
/*SIM_PLAIN[i].Name=(char*)szAttr;*/
strcpy(simulation.SIM_PLAIN[i].Name,(char*)szAttr);

}
szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum");

if(szAttr!=NULL)
{
simulation.SIM_PLAIN[i].Num=atoi((char*)szAttr);

}

i++;

}
FileNodes=FileNodes->next;
}
xmlFree(FileNodes);
simulation.SIM_PLAIN[i].flag=0;

}
if((!xmlStrcmp(szAttr,(const xmlChar *)"Spiral"))){

xmlNodePtr FileNodes=curNode->children;

int i=0;

while(FileNodes!=NULL)
{
if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))
{
szAttr = xmlGetProp(FileNodes,BAD_CAST "name");

if(szAttr!=NULL)
{
/*SIM_SPIRAL[i].Name=(char*)szAttr;*/
strcpy(simulation.SIM_SPIRAL[i].Name,(char*)szAttr);

}
szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum");

if(szAttr!=NULL)
{
simulation.SIM_SPIRAL[i].Num=atoi((char*)szAttr);

}
i++;
}
FileNodes=FileNodes->next;
}
xmlFree(FileNodes);
simulation.SIM_SPIRAL[i].flag=0;

}
if((!xmlStrcmp(szAttr,(const xmlChar *)"axial")))
{
xmlNodePtr FileNodes=curNode->children;

int i=0;

while(FileNodes!=NULL)
{
if((!xmlStrcmp(FileNodes->name,(const xmlChar *)"FILE")))
{
szAttr = xmlGetProp(FileNodes,BAD_CAST "name");

if(szAttr!=NULL)
{
/* SIM_AXIAL[i].Name=(char*)szAttr;*/
strcpy(simulation.SIM_AXIAL[i].Name,(char*)szAttr);

}
szAttr=xmlGetProp(FileNodes,BAD_CAST "sliceNum");

if(szAttr!=NULL)
{
simulation.SIM_AXIAL[i].Num=atoi((char*)szAttr);

}
i++;
}
FileNodes=FileNodes->next;
}
xmlFree(FileNodes);
simulation.SIM_AXIAL[i].flag=0;

}
}
xmlFree(szAttr);

}
curNode = curNode->next;

}

xmlFreeDoc(doc);
/*return 0;*/

}

有什麼不明白的再問吧!

㈡ xml文檔轉換存儲到sql sever資料庫中

分無所謂,如果是你要的看著給吧。不知道你了解多少,我就盡量詳細點,羅嗦點。
表的設計完全是根據XML節點設計的,每一個節點信息都是表的一個欄位,每一個節點的屬性也要是一個表的欄位。
如果是用java寫首先需要導入幾個包
import java.sql.*; //估計一般的項目都是外包連接資料庫,所以用外包資料庫來講。
import java.io.*;
import javax.xml.parsers.*; //這個包是用來解析XML的
import org.w3c.dom.*; //工廠包
import org.apache.crimson.tree.*;//這個包是建立XML的包,也可以選擇不用,一般IDE不自帶,需要自己導入

//類也簡單寫一下吧
public class XmlToSql{
public static void main(string[] args){

//根據XML節點設計出的表欄位,我先假設有3個欄位
String id,username,password;
//建立資料庫連接,簡單方法連一下吧

Class.forName("com.microsoft.jabc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename="資料庫名","登陸名","密碼");
//建立了連接後要用工廠去獲得Document,也就是你的XML
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//建立一個新工廠
DocumentBuilder builder=factory.newDocumentBuilder();//工廠建立一個builder來管理Doc
Document doc=builder.parse(new FileInputStrem(new File("*****.xml"));//把你的XML進行解析放入Document類型文檔。這時候你的XML文檔節點就以樹的形式放入內存了(說白了就是你表的欄位),下面你要什麼就調用就行了。

//開始正式解析文檔
NodeList nodes=doc.getElementsByTagName("a");//從英文也看的出這是把名為a的節點的信息放入NodeList容器中。NodeList就是存放節點組的容器
for(int i=0;i<nodes.getLength(),i++){ //開始循環把你的節點信息插入你的SQL表中,我只需要把你要的欄位提取出來就行了。我們不是前面假定了一個屬性ID,兩個元素節點嗎?這里就來取出
Element node=(Element)nodes.item(i); //先做個元素對象,畢竟我們要的欄位都是元素節點
id=node.getAttributes().getNamedItem("id").getNodeValue(); //用這個方法取出XML中 屬性ID的內容放入ID欄位中,這個ID就是你SQL里的ID名了,最後插入就可以了。注意這里是提取屬性的方法
username=node.getElementsBtagName("username").item(0).getFirstChild().getNode.Value();//提取元素節點username的值放入欄位,.item(0)就是第一個這個欄位,如果就有一個可以不要。
password=node.getElementsBtagName("password").item(0).getFirstChild().getNode.Value();//都是元素節點,所以同樣的方法取出password的值

//好了我們要的3個表值都取到了,並放入了變數中。現在一個SQL語句插入表就行了,其實我很怕在JAVA中寫SQL,不能格式化, .NET就方便的多。都寫到這里了就寫完吧

sql="insert into a values("+id+"','"+username+"'+'"password+"')";
con.createStatement().executeUpdate(sql);

}catch(Exception e{e.printStackTrace();}

}
}

//好了,你的XL信息循環插入表中了,其實XML結構很關鍵,這里是但節點循環,有時候出現多子節點,或者非正規體那就要根據XML的結構來循環輸入了,不過大體方法和思路都是一樣的。由於直接就在這里寫的代碼,沒在IDE中寫,所以括弧匹配方面也許會出小問題,見諒

㈢ asp.net,C#如何操作SqlServer中的XML類型數據

從xml文件中讀出數據,然後綁定到gridview裡面就好了啊給你段代碼,希望對你有所幫助using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;public partial class XML_SQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//private string str = "server=97514BD2A5C9474;database=a;uid=sa;pwd=;";
//private XmlDataDocument doc;
protected void Button1_Click(object sender, EventArgs e)
{
//XmlDataDocument doc;
//SqlConnection con = new SqlConnection(str);
//con.Open();
//SqlDataAdapter da = new SqlDataAdapter("select * from 結果", con);
DataSet ds = new DataSet();
ds.ReadXml("c:\\sample.xml");
//da.Fill(ds, "結果");
//ds.WriteXml("c:\\sample.xml", XmlWriteMode.WriteSchema); dg.DataSource = ds;
//doc = new XmlDataDocument(ds);
//XmlNodeList xlist = doc.GetElementsByTagName("結果");
dg.DataBind();
TextBox1.Text = "";
foreach (DataTable dt in ds.Tables)
{
TextBox1.Text += dt.TableName + "\r\n";
foreach (DataColumn col in dt.Columns)
{
TextBox1.Text += "\t" + col.ColumnName + "-" + col.DataType.FullName + "\r\n";
}
}
}
}

熱點內容
qq群里上傳的文件怎麼刪除 發布:2024-04-24 22:13:21 瀏覽:357
途岳配置升級了什麼 發布:2024-04-24 21:55:55 瀏覽:886
刷機安卓10狀態欄圓角如何修復 發布:2024-04-24 21:24:00 瀏覽:135
創建sql資料庫的命令是什麼 發布:2024-04-24 21:23:58 瀏覽:43
不是安卓該如何下載畫質怪獸 發布:2024-04-24 21:18:51 瀏覽:701
php載入網頁 發布:2024-04-24 21:15:20 瀏覽:161
遠程伺服器更換ip地址 發布:2024-04-24 21:14:43 瀏覽:528
什麼時候上線華為方舟編譯器軟體 發布:2024-04-24 21:10:21 瀏覽:631
安卓布局文件指的是哪個 發布:2024-04-24 21:08:05 瀏覽:889
磁帶存儲價格 發布:2024-04-24 21:04:22 瀏覽:902