xml基本使用
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和BSTR这两种类型是COM中使用的数据类型。
下面的很常用:
_bstr_t bstrName = _bstr_t("name");
char name[30];
strcpy(name, LPCSTR bstrname);