void AddWhiteSpaceToNode(IXMLDOMDocument* pDom,IXMLDOMElementPtr xmlNode,BSTR bstrWs)
{
IXMLDOMText *pws=NULL;
IXMLDOMNode *pBuf=NULL;
pDom->raw_createTextNode(bstrWs,&pws);
xmlNode->raw_appendChild(pws,&pBuf);
if(pws) pws->Release();
pws=NULL;
if(pBuf)pBuf->Release();
pBuf=NULL;
}
在VC中应用MSXML DOM 的一些基本实现方法已知变量
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);
阅读(2947) | 评论(0) | 转发(0) |