请教在vc中把xml文件导入数据库的方法?

解决方案 »

  1.   

    先读取xml各个节点数据,然后将数据写入数据库表格的相应字段。
      

  2.   

    不好意思,以前没有用过xml,是不是用msxml读取各个节点数据,
    但是在数据库中(sql server 2000)中没有相应的表的定义,是不是也可以这样做?
      

  3.   

    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;
      

  4.   

    msdn 2005中用xmlDom的文档。看看就会用了。
    SQL中没对应的表,只能动态创建了。