MSXML4
//引入msxml4.dll
#import "C:/WINNT.0/system32/msxml4.dll"
//创建XMLDOMDocument指针
MSXML2::IXMLDOMDocumentPtr pXMLDoc
// 初始化COM接口
::CoInitialize(NULL)
// 通过智能指针创建一个解析器的实例
HRESULT hr
hr =pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30))
//加载文件
pXMLDoc->load("f://he.xml")
//在树中查找名为City的节点,"//"表示在任意一层查找
MSXML2::IXMLDOMElementPtr childNode
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//AUTHOR"))
//得到节点类型
MSXML2::DOMNodeType nodeType
childNode->get_nodeType(&nodeType)
//节点名称
BSTR var
CString name
childNode->get_nodeName(&var)
name = (char*)(_bstr_t)var
//节点值
VARIANT varVal
childNode->get_nodeTypedValue(&varVal)
CString strValue = (char*)(_bstr_t)varVal
读属性:
//节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL
MSXML2::IXMLDOMNodePtr pAttrItem
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//NUM"))
childNode->get_attributes(&pAttrs)
long nCount
pAttrs->get_length(&nCount)
for(int i = 0 i < nCount i++)
{
pAttrs->get_item(i,&pAttrItem)
//我们可以通过函数get_nodeName,get_nodeTypedValue得到属性名和属性值
//也可以直接得到
CString strAttrName = (char*)(_bstr_t)pAttrItem->nodeName
CString strAttrValue = (char*)(_bstr_t)pAttrItem->nodeTypedValue
}
另外
可以用MSXML DOM
创建一个文档对象的过程。
HRESULT hr
IXMLDomDocument* pXMLDoc
IXMLDOMNode* pXDN
Hr=CoInitialize(NULL) //COM的初始化
hr=CoCreateInstance(CLSID_DOM Document,NULL,CLSCTX_INPPROC_SERVER,
IID_IXMLDOMDocument,(void**) &pXMLDoc)
hr=pXMLDoc->QueryInterface (IID_IXMLDOMNode,(void**)&pXDN)
使用文档中的createElement方法来创建一个节点装载和保存XML文件。通过load或者是loadXML方法可以从一个指定的URL来装载一个XML文档Save方法是用来把文档保存到一个指定的位置,用getElementsByTagName获得制定节点数据
二、
// 创建对象
CoInitialize(NULL)// 初始化COM
// 创建解析器实例
CComPtr<IXMLDOMDocument>spXMLDOM
BSTR bstrItemText
//long value
CString strbuf
HRESULT hr=spXMLDOM.CoCreateInstance (_uuidof(DOMDocument))
spXMLDOM->put_async(VARIANT_FALSE)
spXMLDOM->put_validateOnParse(VARIANT_FALSE)
spXMLDOM->put_resolveExternals(VARIANT_FALSE)
spXMLDOM->put_preserveWhiteSpace(VARIANT_TRUE)
_bstr_t path = _bstr_t(xmlPath)
// 装载XML文档。
hr=spXMLDOM->load(path)
// 1、读取里面全部文本
spXMLDOM->get_text(&bstrItemText)// new
strbuf = bstrItemText// new
// 2、读取一个节点
_bstr_t bstrSS("paragraph")
CComPtr<IXMLDOMNodeList>spParagraphs
spParagraphs=spXMLDOM->getElementsByTagName(bstrSS)//搜索"Paragrah"
//是否正确的得到了指向IDOMNodeList的指针。
hr = spParagraphs->get_length(&value)//得到所包含的NODE节点的个数
if(SUCCEEDED(hr))
...{
spParagraphs->reset()
for(int ii=0ii<valueii++)
...{
CComPtr<IXMLDOMNode>spParagraph
//得到具体的一个NODE节点
//hr = spParagraphs->get_item(ii,&spParagraph)
spParagraph = spParagraphs->nextNode()
if(SUCCEEDED(hr))
...{
spParagraph->get_text(&bstrItemText)//得到该节点相关的文本信息
strbuf += bstrItemText
strbuf += " "
}
}
}
MSXML2::IXMLDOMDocumentPtr pDoc//XML文档接口指针public:
void AddKey(CString moduleName,CString keyName, CString keyValue)
CString ReadKeyValue(CString moduleName, CString keyName)
private:
void NewDocument(CString moduleName, CString keyName, CString keyValue)
public:
bool ReadTable(CTreeCtrl * tree)
bool ReadFields(CString tableName,vector<FIELD>&fields)
bool SaveTable(vector<FIELD>fields,CString tableName,bool bNew)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)