可能水平不够,我也没能改出来
已发送不过,我的只有两层,你看着办吧
还有上面一楼的那个,你根本就是文件 *** 作(fileopen)xml有自己的接口
你要用文件 *** 作的话,如果是下面的格式:
?xml
version="1.0"
encoding="utf-8"?
-
-
-
-
//这边有变化
这种格式你就根本不能读取指定节点,你会12个数据都读出,而不是6个
写MSXML2::IXMLDOMDocumentPtr pDoc
MSXML2::IXMLDOMElementPtr xmlRoot
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30))
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库")
return
}
pDoc->raw_createElement((_bstr_t)(char*)"china",&xmlRoot)
pDoc->raw_appendChild(xmlRoot,NULL)
MSXML2::IXMLDOMElementPtr childNode
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode)
childNode->put_text((_bstr_t)(char*)"WuHan")
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("8,000,000"))
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("10000"))
xmlRoot->appendChild(childNode)
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode)
childNode->put_text((_bstr_t)(char*)"ShangHai")
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("12,000,000"))
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("12000"))
xmlRoot->appendChild(childNode)
pDoc->save("e:\\he.xml")
读
MSXML2::IXMLDOMDocumentPtr pDoc
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30))
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库")
return
}
pDoc->load("e:\\he.xml")
MSXML2::IXMLDOMElementPtr childNode
childNode=(MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"))
MSXML2::DOMNodeType nodeType
childNode->get_nodeType(&nodeType)
BSTR var
childNode->get_nodeName(&var)
CString 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->get_attributes(&pAttrs)
long nCount
pAttrs->get_length(&nCount)
for(int i=0i<nCounti++)
{
pAttrs->get_item(i,&pAttrItem)
CString strAttrName=(char*)(_bstr_t)pAttrItem->nodeName
CString strAttrValue=(char*)(_bstr_t)pAttrItem->nodeTypedValue
AfxMessageBox(strAttrName+strAttrValue)
}
把上面的代码放到两个按钮下就可以了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)