3,055
社区成员
发帖
与我相关
我的任务
分享
<China>
<City population="7000" area="2000">shanghai</City>
<City population="39999" area="3322">beijing</City>
</China>
#import "C:\\WINDOWS\\system32\\msxml3.dll"
using namespace MSXML2;
void COperateXMLDlg::OnBtnGetXML()
{
m_list.DeleteAllItems(); //m_list是ListControl控件绑定的一个变量
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(hr))
{
MessageBox("Error!");
return;
}
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
//注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
if( S_OK != loadrs )
MessageBox("加载XML错误");
MSXML2::IXMLDOMElementPtr childNode;
childNode = (MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
//查询字串中的“//”表示所有行查找
MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML2::IXMLDOMNodeListPtr nodeList;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode->get_attributes(&pAttrs);
childNode->get_childNodes(&nodeList);
long nCount, iCount;
pAttrs->get_length(&nCount); //获取节点属性个数
nodeList->get_length(&iCount); //获取节点个数
for(int i = 0; i < iCount; i++) //根据需要可以添加数据到ListControl中
{
for(int j = 0; j < nCount; j++)
{
pAttrs->get_item(j, &pAttrItem);
CString strAttrName = (char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue = (char*)(_bstr_t)pAttrItem->nodeTypedValue;
m_list.InsertItem(i, strAttrName);
m_list.SetItemText(i, 1, strAttrValue);
}
}
}
inline VARIANT_BOOL IXMLDOMDocument::load ( const _variant_t & xmlSource ) {
VARIANT_BOOL _result = 0;
HRESULT _hr = raw_load(xmlSource, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}
virtual HRESULT __stdcall raw_load (
VARIANT xmlSource,
VARIANT_BOOL * isSuccessful ) = 0;
pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
仅有一个参数的load函数。
你所说的“VARIANT_TRUE和VARIANT_FALSE”是由有两个参数的load函数的第二参数返回的值。我不明白,这两个函数之间返回的值的差别到底是怎么回事!能把你查MSDN的内容粘上来看看吗?IXMLDOMDocument::load Visual Studio 2010
The load method loads an XML document with the contents of the file at the specified location.
HRESULT load(
VARIANT varXmlSource,
VARIANT_BOOL* varIsSuccessful
);
Arguments
varXmlSource
[in] VARIANT containing a URL that specifies the location of the XML file. See Remarks.
bIsSuccessful
[out] Pointer to a VARIANT_BOOL indicating whether the load was successful.
Return Value
If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.
如果成功返回“S_OK”,我代码中的判断也是“S_OK”啊:
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
//注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
if( S_OK != loadrs )
MessageBox("加载XML错误");
这个地方的“loadrs”值是-1,但是S_OK定义的值是1,主要是后面的代码没有影响,xml文档中的信息也读取出来了!
[/quote]lz注意看load函数的返回值是VARIANT_BOOL,并不是HRESULT,再仔细读读MSDN文档,验证是否正确加载用VARIANT_TRUE和VARIANT_FALSE,并不是S_OKIXMLDOMDocument::load Visual Studio 2010
The load method loads an XML document with the contents of the file at the specified location.
HRESULT load(
VARIANT varXmlSource,
VARIANT_BOOL* varIsSuccessful
);
Arguments
varXmlSource
[in] VARIANT containing a URL that specifies the location of the XML file. See Remarks.
bIsSuccessful
[out] Pointer to a VARIANT_BOOL indicating whether the load was successful.
Return Value
If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.
如果成功返回“S_OK”,我代码中的判断也是“S_OK”啊:
HRESULT loadrs = pDoc->load("G:\\XML读写\\OperateXML\\test.xml");
//注释1:一直有异常,loadrs的值为-1,但是其后的代码没有问题,数据读取也正常
if( S_OK != loadrs )
MessageBox("加载XML错误");
这个地方的“loadrs”值是-1,但是S_OK定义的值是1,主要是后面的代码没有影响,xml文档中的信息也读取出来了!
/* 0 == FALSE, -1 == TRUE */
typedef short VARIANT_BOOL;