已知变量
MSXML2::IXMLDOMDocument *pDoc;
MSXML2::IXMLDOMNode *pChild, *pParent;
MSXML2::IXMLDOMNode *pNod;
MSXML2::IXMLDOMElement *pEle;
初始化指针:
MSXML2::IXMLDOMDocument *pDocument=NULL;
MSXML2::IXMLDOMNodeList *pNodeList=NULL;
MSXML2::IXMLDOMNamedNodeMap *pNodeMap=NULL;
MSXML2::IXMLDOMNode *pNode=NULL
MSXML2::IXMLDOMText *pText=NULL;
MSXML2::IXMLDOMElement *pElement=NULL;
MSXML2::IXMLDOMProcessingInstruction *pProcessingInstruction=NULL;
MSXML2::IXMLDOMComment *pComment=NULL;
MSXML2::IXMLDOMParseError *pObjError = NULL;
已知数据:
BSTR bstrText, bstrName;
int type;
**)创建新文档
// hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
// IID_IXMLDOMDocument,(void**)&m_pXMLDoc);
HRESULT hr=CoCreateInstance(__uuidof(MSXML2::DOMDocument40),NULL,CLSCTX_INPROC_SERVER,
__uuidof(MSXML2::IXMLDOMDocument),(void**)&m_pXMLDoc);
hr = pDocument->put_async(VARIANT_FALSE);
hr = pDocument->put_validateOnParse(VARIANT_FALSE);
hr = pDocument->put_resolveExternals(VARIANT_FALSE);
a)添加子节点到父节点
pParent->appendChild(pChild, &pNode)
b)创建节点
VARIANT vtype;
vtype.vt = VT_I4;
V_I4(&vtype) = (int)type;
pDoc->createNode(vtype, bstrName, NULL, &pNode);
c)创建元素节点
pDoc->createElement(bstrName,pElement);
(pElement)->put_text(bstrText);
d)创建文本子节点,并添加到父节点中
pDoc->createTextNode(bstrText,&pText);
pParent->appendChild(pText,& pNode)
e)创建// Create a processing instruction element.
BSTR bstrTarget = SysAllocString(L"xml");
BSTR bstrData = SysAllocString(L"version='1.0'");
pDoc ->createProcessingInstruction(bstrTarget, bstrData, &pProcessingInstruction);
SysFreeString(bstrTarget);
SysFreeString(bstrData);
f)创建注释节点
pDoc->createComment(bstrText, &pComment);
g)元素节点属性值
得到属性值
VARIANT v;
pEle->getAttribute(bstrName,&v);
CString str = v.bstrVal;
设置属性值
CComVariant v(str);
pEle->setAttribute(bstrName, v);
h)节点属性值
VARIANT v;
CString str;
long mCount;
得到节点属性集
hr=pNod->get_attributes(&pNodeMap);
hr=pNodeMap->get_length(&mCount);
得到节点属性
hr=pNodeMap->getNamedItem(bstrName,&pNode);
pNodeMap->get_item(i,&pNode);
得到节点属性值
hr=pNode->get_nodevalue(&v);
str =v.bstrVal;
删除节点属性
MSXML2::IXMLDOMNode *moldNode;
mNodeMape->removeNamedItem(bstrName,&moldNode);
if (moldNode!=NULL) moldNode->Release();
i)节点
(1)得到节点、节点集
根据节点名称
pDoc->getElementsByTagName(bstrName,&pNodeList);
pNodeList->get_item(0,&pNode);
得到子节点集
hr=pNod->get_childNodes(&mNodeList);
long mCount;
MSXML2::IXMLDOMNode *pNodeSub;
mNodeList->get_length(&mCount);
hr=pNode->get_firstChild(&pNodeSub);
删除子节点(包含删除节点属性、子节点、当前节点)
删除当前子节点
pNodeList->get_item(i,&pNode);
pNod->removeChild(pNode,&moldNode);
moldNode->Release();
(2)根据节点得到节点名称
pNod->get_nodeName(&bstrName);
(3)根据节点得到节点值
VARIANT v;
hr=pNod->get_nodevalue(&v);
j)XML文件操作
加载
VARIANT_BOOL status;
VARIANT vFileName;
(1)
pDoc->loadXML(bstrName,&status);
(2)
V_BSTR(&vFileName) = strFileName.AllocSysString();
V_VT(&vFileName) = VT_BSTR;
vResult = pDoc->load(vFileName,&status);
保存
BSTR pBFName = mFileName.AllocSysString();
CComVariant v(pBFName);
hr=pDoc->save(v);
BSTR bstr = NULL;
pDoc->get_xml(&bstr);
k)错误处理
BSTR bstr = NULL;
pDoc->get_parseError(&pObjError);
pObjError->get_reason(&bstr);
AfxMessageBox(_T("Failed to load DOM from books.xml. %S/n"),*bstr);
=======================88888888888=======================================
现在XML文档应用的方面特别的多.
我把以前写过的一个例子帖出来,以备以后使用.
第一部分:DOM解析:
概述:DOM解析将会把一个完整的XML文档读进来,生成一个结构树。这样会要把XML文档全部都加载到内在中。所以解析起来的速度会要慢一些。
1、如何加载xml文件:
//创建DOM,加载XML文档
MSXML::IXMLDOMDocumentPtr pCommandDoc;
pCommandDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
pCommandDoc->put_async(VARIANT_FALSE);
pCommandDoc->put_validateOnParse(VARIANT_FALSE);
pCommandDoc->put_resolveExternals(VARIANT_FALSE);
pCommandDoc->put_preserveWhiteSpace(VARIANT_TRUE);
pCommandDoc->load(file.GetBuffer(0));
2、在XML文档中查找指定的结点:
//找到
MSXML::IXMLDOMNodePtr pRootNode=pCommandDoc->selectSingleNode("root/record");
if (pRootNode==NULL)
{
return ;
}
3、得到XML文档中,结点的属性
CString strTemp;
MSXML::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
pRootNode->get_attributes(&pAttrs);
if (pAttrs==NULL)
{
return;
}
MSXML::IXMLDOMNodePtr pRequestTypeAttr=pAttrs->getQualifiedItem("name","");
_bstr_t strRequestType=pRequestTypeAttr->Gettext();
strTemp=strRequestType.operator char *();
4、得到结点的内容
_bstr_t strVisiPort=pNode->Gettext();
5、设置结点的内容
HRESULT hr=pNode->put_text(_bstr_t(m_strGatewayPassword));