码迷,mamicode.com
首页 > 其他好文 > 详细

xml基本使用

时间:2015-05-13 06:04:13      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

void CRgnTestDlg::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码    ::CoInitialize(NULL);
    CComPtr<MSXML2::IXMLDOMDocument> pDoc;
    pDoc.CoCreateInstance(_uuidof(MSXML2::DOMDocument));
    pDoc->load("test.xml");
    CComPtr<MSXML2::IXMLDOMNodeList> pList;
    pList = pDoc->selectNodes("*");
    int len = pList->Getlength();
    for(int i = 0; i < len; i++)
    {
        CComPtr<MSXML2::IXMLDOMNode> pNode = pList->Getitem(i);
        _bstr_t bstrXml = pNode->Getxml();
        char pch[1024]; 
        strcpy(pch,(LPSTR)bstrXml);
        CComPtr<MSXML2::IXMLDOMDocument> pDoc1;
        pDoc->loadXML(bstrXml);
        CComPtr<MSXML2::IXMLDOMNode> pTest = pDoc->selectSingleNode("//id");
        _bstr_t bstrName = pTest->Gettext();
//        ofstream fout("E:\\my.xml",ios::trunc);
//        fout<<pch;
//        fout.close();
//        MessageBox(str);
        pNode.Release();
    }
    CString str = pDoc->Gettext();
    MessageBox(str);

    pList.Release();
    pDoc.Release();

    ::CoUninitialize();
}


void CRgnTestDlg::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
/*    HDC hdc = ::GetDC(m_hWnd);
    HBRUSH hbr = ::CreateSolidBrush(RGB(255,0,0));
    ::FillRgn(hdc, m_hRgn, hbr);*/

    ::CoInitialize(NULL);
    CComPtr<MSXML2::IXMLDOMDocument> pDoc;

    pDoc.CoCreateInstance(_uuidof(MSXML2::DOMDocument));
    CComPtr<MSXML2::IXMLDOMNode> pHead = pDoc->createNode(NODE_ELEMENT,"XKJY",
        "");
    pDoc->appendChild(pHead);
    CComPtr<MSXML2::IXMLDOMNode> pNode = pDoc->createNode(NODE_ELEMENT,"length",
        "");
    pNode->Puttext(L"");
    pHead->appendChild(pNode);

     pNode = pDoc->createNode(NODE_ELEMENT,"id",
        "");
     pNode->Puttext(L"10086");
    pHead->appendChild(pNode);

    pNode = pDoc->createNode(NODE_ELEMENT,"lat","");
    pNode->Puttext(L"22.83741");
    pHead->appendChild(pNode);

    pNode = pDoc->createNode(NODE_ELEMENT,"lng","");
    pNode->Puttext(L"114.34221");
    pHead->appendChild(pNode);

    pDoc->save("test.xml");
//    char sz[1024];
//    ULONG  len = 0;
//    pS->Write(sz, 1024,&len);
//    CString str(sz);
//    MessageBox(str);
//    pNode1.Release();
    pNode.Release();
    pHead.Release();
    pDoc.Release();
    ::CoUninitialize();
}

#include <msxml.h>
定义接口智能指针:CComPtr<MSXML2::IXMLDOMDocument> pDoc;

pDoc->CoCreateInstance(_uuidof(DOMDocument));

_bstr_t是什么,百度

_variant_t和_bstr_t这两个类分别封装并管理VARIANT和BSTR这两种数据类型
VARIANT和BSTR这两种类型是COM中使用的数据类型。
下面的很常用:
_bstr_t bstrName = _bstr_t("name");
char name[30];
strcpy(name, LPCSTR bstrname);
 

xml基本使用

标签:

原文地址:http://www.cnblogs.com/wangfs/p/4499171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!