请教在vc中把xml文件导入数据库的方法 请教在vc中把xml文件导入数据库的方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先读取xml各个节点数据,然后将数据写入数据库表格的相应字段。 不好意思,以前没有用过xml,是不是用msxml读取各个节点数据,但是在数据库中(sql server 2000)中没有相应的表的定义,是不是也可以这样做? MSXML2::IXMLDOMNodeList *NodeList; MSXML2::IXMLDOMNode* NodeItem; MSXML2::IXMLDOMNamedNodeMap* pXMLNamedNodeMap; CFileFind ffile; CComVariant varFile; CComBSTR value; HRESULT hr; if(!ffile.FindFile(dirFile)) { AfxMessageBox("XML文件不存在"); return FALSE; } try { IXMLDOMDocument2Ptr xmlDoc(__uuidof(DOMDocument40)); xmlDoc->async =false; varFile = dirFile; hr = xmlDoc->load(varFile); CComPtr<MSXML2::IXMLDOMElement> spElement; hr = xmlDoc->get_documentElement(&spElement); if(hr == S_FALSE) { AfxMessageBox("解析根结点失败"); return FALSE; } hr = spElement->get_childNodes(&NodeList); if(hr == S_FALSE) { AfxMessageBox("解析结点失败"); return FALSE; } spElement->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); if(m_isyesfXML == FALSE) { AfxMessageBox("请选择正确类型的XML文件"); return FALSE; } spElement->get_childNodes(&NodeList); long num; NodeList->get_length(&num); for( int i = 0; i < num ; i++ ) { NodeList->get_item(i, &NodeItem); NodeItem->get_nodeName(&value); if(value == "batch") { MSXML2::IXMLDOMNodeList *NodeList1; NodeItem->get_childNodes(&NodeList1); NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); long num1; NodeList1->get_length(&num1); for(int i = 0; i < num1 ; i++) { NodeList1->get_item(i, &NodeItem); NodeItem->get_nodeName(&value); if(value == "base-file") { NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); } if(value == "priority-claims") { MSXML2::IXMLDOMNodeList *NodeList2; NodeItem->get_childNodes(&NodeList2); NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); long num2; NodeList2->get_length(&num2); for( int i = 0; i < num2; i++ ) { NodeList2->get_item(i, &NodeItem); NodeItem->get_nodeName(&value); NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); } NodeList2->Release(); } } NodeList1->Release(); } } NodeList->Release(); } catch(_com_error &err) { CString strErr = "获得信息失败\n"; strErr += (const char *)err.Description(); AfxMessageBox(strErr); return FALSE; } return TRUE; msdn 2005中用xmlDom的文档。看看就会用了。SQL中没对应的表,只能动态创建了。 怎么暂停一下While循环 static控件换行问题 单文档框架,分隔条失败! 如何获取指定Gif(Jpg)文件的属性(大小, xxx*yyy)? //知道一个窗口的句柄,如何得到它所属的进程ID?// 请问一个程序无响应的问题! 怎样解除WORD文档的打开权限密码? 请高手指教关于Microsoft Visual Studio .NET 2003通信的问题 如何用ADO打开数据库??(不要笑我 :P) ★★★★★★★紧急求救★★★★★★★★ 请教关于multimedia timers的用法 我用水晶报表10做的程序(VC6.0)怎样发布?
但是在数据库中(sql server 2000)中没有相应的表的定义,是不是也可以这样做?
if(!ffile.FindFile(dirFile))
{
AfxMessageBox("XML文件不存在"); return FALSE;
} try
{
IXMLDOMDocument2Ptr xmlDoc(__uuidof(DOMDocument40)); xmlDoc->async =false; varFile = dirFile; hr = xmlDoc->load(varFile);
CComPtr<MSXML2::IXMLDOMElement> spElement; hr = xmlDoc->get_documentElement(&spElement); if(hr == S_FALSE)
{
AfxMessageBox("解析根结点失败");
return FALSE;
} hr = spElement->get_childNodes(&NodeList); if(hr == S_FALSE)
{
AfxMessageBox("解析结点失败"); return FALSE;
} spElement->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap); if(m_isyesfXML == FALSE)
{
AfxMessageBox("请选择正确类型的XML文件");
return FALSE;
}
spElement->get_childNodes(&NodeList); long num; NodeList->get_length(&num);
for( int i = 0; i < num ; i++ )
{
NodeList->get_item(i, &NodeItem); NodeItem->get_nodeName(&value); if(value == "batch")
{
MSXML2::IXMLDOMNodeList *NodeList1; NodeItem->get_childNodes(&NodeList1); NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap);
long num1; NodeList1->get_length(&num1);
for(int i = 0; i < num1 ; i++)
{
NodeList1->get_item(i, &NodeItem); NodeItem->get_nodeName(&value);
if(value == "base-file")
{
NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap);
} if(value == "priority-claims")
{
MSXML2::IXMLDOMNodeList *NodeList2; NodeItem->get_childNodes(&NodeList2); NodeItem->get_attributes(&pXMLNamedNodeMap);
GetNodeAttributes(pXMLNamedNodeMap); long num2; NodeList2->get_length(&num2);
for( int i = 0; i < num2; i++ )
{
NodeList2->get_item(i, &NodeItem);
NodeItem->get_nodeName(&value); NodeItem->get_attributes(&pXMLNamedNodeMap); GetNodeAttributes(pXMLNamedNodeMap);
}
NodeList2->Release();
}
}
NodeList1->Release();
}
} NodeList->Release();
}
catch(_com_error &err)
{
CString strErr = "获得信息失败\n"; strErr += (const char *)err.Description(); AfxMessageBox(strErr); return FALSE;
} return TRUE;
SQL中没对应的表,只能动态创建了。